JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.model;
import org.apache.commons.jxpath.AbstractFactory;
import org.apache.commons.jxpath.IdentityManager;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.JXPathTestCase;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.Variables;
import org.apache.commons.jxpath.xml.DocumentContainer;
/**
* Abstract superclass for pure XPath 1.0. Subclasses
* apply the same XPaths to contexts using different models:
* DOM, JDOM etc.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public abstract class XMLModelTestCase extends JXPathTestCase {
protected JXPathContext context;
/**
* Construct a new instance of this test case.
*
* @param name Name of the test case
*/
public XMLModelTestCase(String name) {
super(name);
}
public void setUp() {
if (context == null) {
DocumentContainer docCtr = createDocumentContainer();
context = createContext();
Variables vars = context.getVariables();
vars.declareVariable("document", docCtr.getValue());
vars.declareVariable("container", docCtr);
vars.declareVariable(
"element",
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>",
"John Doe",
"/vendor[1]/location[1]/@manager");
assertXPathCreatePathAndSetValue(
context,
"/vendor[1]/location[4]/@manager",
"James Dow",
"/vendor[1]/location[4]/@manager");
assertXPathCreatePathAndSetValue(
context,
"vendor/product/product:name/attribute::price:language",
"English",
"/vendor[1]/product[1]/product:name[1]/@price:language");
context.registerNamespace("price", "priceNS");
// Create a DOM element
assertXPathCreatePathAndSetValue(
context,
"/vendor[1]/price:foo/price:bar",
"123.20",
"/vendor[1]/price:foo[1]/price:bar[1]");
}
/**
* Test JXPathContext.removePath() with various arguments
*/
public void testRemovePath() {
// Remove XML nodes
context.removePath("vendor/location[@id = '101']//street/text()");
assertEquals(
"Remove DOM text",
"",
context.getValue("vendor/location[@id = '101']//street"));
context.removePath("vendor/location[@id = '101']//street");
assertEquals(
"Remove DOM element",
new Double(0),
context.getValue("count(vendor/location[@id = '101']//street)"));
context.removePath("vendor/location[@id = '100']/@name");
assertEquals(
"Remove DOM attribute",
new Double(0),
context.getValue("count(vendor/location[@id = '100']/@name)"));
}
public void testID() {
context.setIdentityManager(new IdentityManager() {
public Pointer getPointerByID(JXPathContext context, String id) {
NodePointer ptr = (NodePointer) context.getPointer("/");
ptr = ptr.getValuePointer(); // Unwrap the container
return ptr.getPointerByID(context, id);
}
});
assertXPathValueAndPointer(
context,
"id(101)//street",
"Tangerine Drive",
"id('101')/address[1]/street[1]");
assertXPath
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import org.apache.commons.jxpath.util.KeyManagerUtils;
/**
* JXPathContext provides APIs for the traversal of graphs of JavaBeans using
* the XPath syntax. Using JXPathContext, you can read and write properties of
* JavaBeans, arrays, collections and maps. JXPathContext uses JavaBeans
* introspection to enumerate and access JavaBeans properties.
* <p>
* JXPathContext allows alternative implementations. This is why instead of
* allocating JXPathContext directly, you should call a static
* <code>newContext</code> method. This method will utilize the
* JXPathContextFactory API to locate a suitable implementation of JXPath.
* Bundled with JXPath comes a default implementation called Reference
* Implementation.
* </p>
*
* <h2>JXPath Interprets XPath Syntax on Java Object Graphs</h2>
*
* JXPath uses an intuitive interpretation of the xpath syntax in the context
* of Java object graphs. Here are some examples:
*
* <h3>Example 1: JavaBean Property Access</h3>
*
* JXPath can be used to access properties of a JavaBean.
*
* <
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>;
protected AbstractFactory factory;
private Locale locale;
private boolean lenientSet = false;
private boolean lenient = false;
protected IdentityManager idManager;
protected KeyManager keyManager;
protected HashMap decimalFormats;
private static JXPathContextFactory contextFactory;
private static JXPathContext compilationContext;
private static final PackageFunctions GENERIC_FUNCTIONS =
new PackageFunctions("", null);
/**
* Creates a new JXPathContext with the specified object as the root node.
*/
public static JXPathContext newContext(Object contextBean) {
return getContextFactory().newContext(null, contextBean);
}
/**
* Creates a new JXPathContext with the specified bean as the root node and
* the specified parent context. Variables defined in a parent context can
* be referenced in XPaths passed to the child context.
*/
public static JXPathContext newContext(
JXPathContext parentContext,
Object contextBean)
{
return getContextFactory().newContext(parentContext, contextBean);
}
/**
* Acquires a context factory and caches it.
*/
private static JXPathContextFactory getContextFactory () {
if (contextFactory == null) {
contextFactory = JXPathContextFactory.newInstance();
}
return contextFactory;
}
/**
* This constructor should remain protected - it is to be overridden by
* subclasses, but never explicitly invoked by clients.
*/
protected JXPathContext(JXPathContext parentContext, Object contextBean) {
this.parentContext = parentContext;
this.contextBean = contextBean;
}
/**
* Returns the parent context of this context or null.
*/
public JXPathContext getParentContext() {
return parentContext;
}
/**
* Returns the JavaBean associated with this context.
*/
public Object getContextBean() {
return contextBean;
}
/**
* Returns a Pointer for the context bean.
*/
public abstract Pointer getContextPointer();
/**
* Returns a JXPathContext that is relative to the current JXPathContext.
* The supplied pointer becomes the context pointer of the new context.
* The relative context inherits variables, extension functions, locale etc
* from the parent context.
*/
public abstract JXPathContext getRelativeContext(Pointer pointer);
/**
* Installs a custom implementation of the
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> return compilationContext.compilePath(xpath);
}
/**
* Overridden by each concrete implementation of JXPathContext
* to perform compilation. Is called by <code>compile()</code>.
*/
protected abstract CompiledExpression compilePath(String xpath);
/**
* Finds the first object that matches the specified XPath. It is equivalent
* to <code>getPointer(xpath).getNode()</code>. Note that this method
* produces the same result as <code>getValue()</code> on object models
* like JavaBeans, but a different result for DOM/JDOM etc., because it
* returns the Node itself, rather than its textual contents.
*
* @param xpath the xpath to be evaluated
* @return the found object
*/
public Object selectSingleNode(String xpath) {
Pointer pointer = getPointer(xpath);
return pointer == null ? null : pointer.getNode();
}
/**
* Finds all nodes that match the specified XPath.
*
* @param xpath the xpath to be evaluated
* @return a list of found objects
*/
public List selectNodes(String xpath) {
ArrayList list = new ArrayList();
Iterator iterator = iteratePointers(xpath);
while (iterator.hasNext()) {
Pointer pointer = (Pointer) iterator.next();
list.add(pointer.getNode());
}
return list;
}
/**
* Evaluates the xpath and returns the resulting object. Primitive
* types are wrapped into objects.
*/
public abstract Object getValue(String xpath);
/**
* Evaluates the xpath, converts the result to the specified class and
* returns the resulting object.
*/
public abstract Object getValue(String xpath, Class requiredType);
/**
* Modifies the value of the property described by the supplied xpath.
* Will throw an exception if one of the following conditions occurs:
* <ul>
* <li>The xpath does not in fact describe an existing property
* <li>The property is not writable (no public, non-static set method)
* </ul>
*/
public abstract void setValue(String xpath, Object value);
/**
* Creates missing elements of the path by invoking an AbstractFactory,
* which should first be installed on the context by calling "setFactory".
* <p>
* Will throw an exception
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> if the AbstractFactory fails to create
* an instance for a path element.
*/
public abstract Pointer createPath(String xpath);
/**
* The same as setValue, except it creates intermediate elements of
* the path by invoking an AbstractFactory, which should first be
* installed on the context by calling "setFactory".
* <p>
* Will throw an exception if one of the following conditions occurs:
* <ul>
* <li>Elements of the xpath aleady exist, but the path does not in
* fact describe an existing property
* <li>The AbstractFactory fails to create an instance for an intermediate
* element.
* <li>The property is not writable (no public, non-static set method)
* </ul>
*/
public abstract Pointer createPathAndSetValue(String xpath, Object value);
/**
* Removes the element of the object graph described by the xpath.
*/
public abstract void removePath(String xpath);
/**
* Removes all elements of the object graph described by the xpath.
*/
public abstract void removeAll(String xpath);
/**
* Traverses the xpath and returns an Iterator of all results found
* for the path. If the xpath matches no properties
* in the graph, the Iterator will be empty, but not null.
*/
public abstract Iterator iterate(String xpath);
/**
* Traverses the xpath and returns a Pointer.
* A Pointer provides easy access to a property.
* If the xpath matches no properties
* in the graph, the pointer will be null.
*/
public abstract Pointer getPointer(String xpath);
/**
* Traverses the xpath and returns an Iterator of Pointers.
* A Pointer provides easy access to a property.
* If the xpath matches no properties
* in the graph, the Iterator be empty, but not null.
*/
public abstract Iterator iteratePointers(String xpath);
/**
* Install an identity manager that will be used by the context
* to look up a node by its ID.
*/
public void setIdentityManager(IdentityManager idManager) {
this.idManager = idManager;
}
/**
* Returns this context's identity manager. If none has been installed,
* returns the identity manager of the parent context.
*/
public IdentityManager getIdentityManager() {
if (
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>idManager == null && parentContext != null) {
return parentContext.getIdentityManager();
}
return idManager;
}
/**
* Locates a Node by its ID.
*
* @param id is the ID of the sought node.
*/
public Pointer getPointerByID(String id) {
IdentityManager manager = getIdentityManager();
if (manager != null) {
return manager.getPointerByID(this, id);
}
throw new JXPathException(
"Cannot find an element by ID - "
+ "no IdentityManager has been specified");
}
/**
* Install a key manager that will be used by the context
* to look up a node by a key value.
*/
public void setKeyManager(KeyManager keyManager) {
this.keyManager = keyManager;
}
/**
* Returns this context's key manager. If none has been installed,
* returns the key manager of the parent context.
*/
public KeyManager getKeyManager() {
if (keyManager == null && parentContext != null) {
return parentContext.getKeyManager();
}
return keyManager;
}
/**
* Locates a Node by a key value.
*/
public Pointer getPointerByKey(String key, String value) {
KeyManager manager = getKeyManager();
if (manager != null) {
return manager.getPointerByKey(this, key, value);
}
throw new JXPathException(
"Cannot find an element by key - "
+ "no KeyManager has been specified");
}
/**
* Locates a NodeSet by key/value.
* @param key
* @param value
*/
public NodeSet getNodeSetByKey(String key, Object value) {
KeyManager manager = getKeyManager();
if (manager != null) {
return KeyManagerUtils.getExtendedKeyManager(manager)
.getNodeSetByKey(this, key, value);
}
throw new JXPathException("Cannot find an element by key - "
+ "no KeyManager has been specified");
}
/**
* Registers a namespace prefix.
*
* @param prefix A namespace prefix
* @param namespaceURI A URI for that prefix
*/
public void registerNamespace(String prefix, String namespaceURI) {
throw new UnsupportedOperationException(
"Namespace registration is
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> not implemented by " + getClass());
}
/**
* Given a prefix, returns a registered namespace URI. If the requested
* prefix was not defined explicitly using the registerNamespace method,
* JXPathContext will then check the context node to see if the prefix is
* defined there. See
* {@link #setNamespaceContextPointer(Pointer) setNamespaceContextPointer}.
*
* @param prefix The namespace prefix to look up
* @return namespace URI or null if the prefix is undefined.
*/
public String getNamespaceURI(String prefix) {
throw new UnsupportedOperationException(
"Namespace registration is not implemented by " + getClass());
}
/**
* Get the prefix associated with the specifed namespace URI.
* @param namespaceURI the ns URI to check.
* @return String prefix
* @since JXPath 1.3
*/
public String getPrefix(String namespaceURI) {
throw new UnsupportedOperationException(
"Namespace registration is not implemented by " + getClass());
}
/**
* Namespace prefixes can be defined implicitly by specifying a pointer to a
* context where the namespaces are defined. By default,
* NamespaceContextPointer is the same as the Context Pointer, see
* {@link #getContextPointer() getContextPointer()}
*
* @param namespaceContextPointer The pointer to the context where prefixes used in
* XPath expressions should be resolved.
*/
public void setNamespaceContextPointer(Pointer namespaceContextPointer) {
throw new UnsupportedOperationException(
"Namespace registration is not implemented by " + getClass());
}
/**
* Returns the namespace context pointer set with
* {@link #setNamespaceContextPointer(Pointer) setNamespaceContextPointer()}
* or, if none has been specified, the context pointer otherwise.
*
* @return The namespace context pointer.
*/
public Pointer getNamespaceContextPointer() {
throw new UnsupportedOperationException(
"Namespace registration is not implemented by " + getClass());
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath;
import java.io.Serializable;
/**
* Pointers represent locations of objects and their properties
* in Java object graphs. JXPathContext has methods
* ({@link JXPathContext#getPointer(java.lang.String) getPointer()}
* and ({@link JXPathContext#iteratePointers(java.lang.String)
* iteratePointers()}, which, given an XPath, produce Pointers for the objects
* or properties described the the path. For example, <code>ctx.getPointer
* ("foo/bar")</code> will produce a Pointer that can get and set the property
* "bar" of the object which is the value of the property "foo" of the root
* object. The value of <code>ctx.getPointer("aMap/aKey[3]")</code> will be a
* pointer to the 3'rd element of the array, which is the value for the key
* "aKey" of the map, which is the value of the property "aMap" of the root
* object.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public interface Pointer extends Cloneable, Comparable, Serializable {
/**
* Returns the value of the object, property or collection element
* this pointer represents. May convert the value to one of the
* canonical InfoSet types: String, Number, Boolean, Set.
*
* For example,
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that walks the "namespace::" axis.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class NamespaceContext extends EvalContext {
private NodeTest nodeTest;
private boolean setStarted = false;
private NodeIterator iterator;
private NodePointer currentNodePointer;
/**
* @param parentContext represents the previous step on the path
* @param nodeTest is the name of the namespace we are looking for
*/
public NamespaceContext(EvalContext parentContext, NodeTest nodeTest) {
super(parentContext);
this.nodeTest = nodeTest;
}
public NodePointer getCurrentNodePointer() {
return currentNodePointer;
}
public void reset() {
setStarted = false;
iterator = null;
super.reset();
}
public boolean setPosition(int position) {
if (position < getCurrentPosition()) {
reset();
}
while (
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>getCurrentPosition() < position) {
if (!nextNode()) {
return false;
}
}
return true;
}
public boolean nextNode() {
super.setPosition(getCurrentPosition() + 1);
if (!setStarted) {
setStarted = true;
if (!(nodeTest instanceof NodeNameTest)) {
return false;
}
NodeNameTest nodeNameTest = (NodeNameTest) nodeTest;
QName testName = nodeNameTest.getNodeName();
if (testName.getPrefix() != null) {
return false;
}
if (nodeNameTest.isWildcard()) {
iterator =
parentContext.getCurrentNodePointer().namespaceIterator();
}
else {
currentNodePointer =
parentContext.getCurrentNodePointer().namespacePointer(
testName.getName());
return currentNodePointer != null;
}
}
if (iterator == null) {
return false;
}
if (!iterator.setPosition(iterator.getPosition() + 1)) {
return false;
}
currentNodePointer = iterator.getNodePointer();
return true;
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>this.position < position) {
if (!next()) {
return false;
}
}
while (this.position > position) {
if (!previous()) {
return false;
}
}
return true;
}
private boolean previous() {
position--;
if (!reverse) {
if (position == 0) {
child = null;
}
else if (child == null) {
child = node.getLastChild();
}
else {
child = child.getPreviousSibling();
}
while (child != null && !testChild()) {
child = child.getPreviousSibling();
}
}
else {
child = child.getNextSibling();
while (child != null && !testChild()) {
child = child.getNextSibling();
}
}
return child != null;
}
private boolean next() {
position++;
if (!reverse) {
if (position == 1) {
if (child == null) {
child = node.getFirstChild();
}
else {
child = child.getNextSibling();
}
}
else {
child = child.getNextSibling();
}
while (child != null && !testChild()) {
child = child.getNextSibling();
}
}
else {
if (position == 1) {
if (child == null) {
child = node.getLastChild();
}
else {
child = child.getPreviousSibling();
}
}
else {
child = child.getPreviousSibling();
}
while (child != null && !testChild()) {
child = child.getPreviousSibling();
}
}
return child != null;
}
private boolean testChild() {
return DOMNodePointer.testNode(child, nodeTest);
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.model.dom;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.jxpath.AbstractFactory;
import org.apache.commons.jxpath.JXPathAbstractFactoryException;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.Compiler;
import org.apache.commons.jxpath.ri.NamespaceResolver;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
import org.apache.commons.jxpath.ri.compiler.ProcessingInstructionTest;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.apache.commons.jxpath.ri.model.beans.NullPointer;
import org.apache.commons.jxpath.util.TypeUtils;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> the
* node are replaced with the children of the passed node.
*/
public void setValue(Object value) {
if (node.getNodeType() == Node.TEXT_NODE
|| node.getNodeType() == Node.CDATA_SECTION_NODE) {
String string = (String) TypeUtils.convert(value, String.class);
if (string != null && !string.equals("")) {
node.setNodeValue(string);
}
else {
node.getParentNode().removeChild(node);
}
}
else {
NodeList children = node.getChildNodes();
int count = children.getLength();
for (int i = count; --i >= 0;) {
Node child = children.item(i);
node.removeChild(child);
}
if (value instanceof Node) {
Node valueNode = (Node) value;
if (valueNode instanceof Element
|| valueNode instanceof Document) {
children = valueNode.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
node.appendChild(child.cloneNode(true));
}
}
else {
node.appendChild(valueNode.cloneNode(true));
}
}
else {
String string = (String) TypeUtils.convert(value, String.class);
if (string != null && !string.equals("")) {
Node textNode =
node.getOwnerDocument().createTextNode(string);
node.appendChild(textNode);
}
}
}
}
public NodePointer createChild(
JXPathContext context,
QName name,
int index)
{
if (index == WHOLE_COLLECTION) {
index = 0;
}
boolean success =
getAbstractFactory(context).createObject(
context,
this,
node,
name.toString(),
index);
if (success) {
NodeTest nodeTest;
String prefix = name.getPrefix();
String namespaceURI = prefix == null ? null :
context.getNamespaceURI(prefix);
nodeTest = new NodeNameTest(name, namespaceURI);
NodeIterator it = childIterator(nodeTest, false, null);
if (it != null && it.setPosition(index + 1)) {
return it.getNodePointer();
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> }
}
throw new JXPathAbstractFactoryException(
"Factory could not create a child node for path: " + asPath()
+ "/" + name + "[" + (index + 1) + "]");
}
public NodePointer createChild(JXPathContext context,
QName name, int index, Object value)
{
NodePointer ptr = createChild(context, name, index);
ptr.setValue(value);
return ptr;
}
public NodePointer createAttribute(JXPathContext context, QName name) {
if (!(node instanceof Element)) {
return super.createAttribute(context, name);
}
Element element = (Element) node;
String prefix = name.getPrefix();
if (prefix != null) {
String ns = null;
NamespaceResolver nsr = getNamespaceResolver();
if (nsr != null) {
ns = nsr.getNamespaceURI(prefix);
}
if (ns == null) {
throw new JXPathException(
"Unknown namespace prefix: " + prefix);
}
element.setAttributeNS(ns, name.toString(), "");
}
else {
if (!element.hasAttribute(name.getName())) {
element.setAttribute(name.getName(), "");
}
}
NodeIterator it = attributeIterator(name);
it.setPosition(1);
return it.getNodePointer();
}
public void remove() {
Node parent = node.getParentNode();
if (parent == null) {
throw new JXPathException("Cannot remove root DOM node");
}
parent.removeChild(node);
}
public String asPath() {
if (id != null) {
return "id('" + escape(id) + "')";
}
StringBuffer buffer = new StringBuffer();
if (parent != null) {
buffer.append(parent.asPath());
}
switch (node.getNodeType()) {
case Node.ELEMENT_NODE :
// If the parent pointer is not a DOMNodePointer, it is
// the parent's responsibility to produce the node test part
// of the path
if (parent instanceof DOMNodePointer) {
if (buffer.length() == 0
|| buffer.charAt(buffer.length() - 1) != '/') {
buffer.append('/');
}
String ln = DOMNodePointer.getLocalName(node
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
Element element = (Element) node;
String uri = element.getNamespaceURI();
if (uri != null) {
return uri;
}
String prefix = getPrefix(node);
String qname = prefix == null ? "xmlns" : "xmlns:" + prefix;
Node aNode = node;
while (aNode != null) {
if (aNode.getNodeType() == Node.ELEMENT_NODE) {
Attr attr = ((Element) aNode).getAttributeNode(qname);
if (attr != null) {
return attr.getValue();
}
}
aNode = aNode.getParentNode();
}
return null;
}
public Object getValue() {
if (node.getNodeType() == Node.COMMENT_NODE) {
String text = ((Comment) node).getData();
return text == null ? "" : text.trim();
}
return stringValue(node);
}
private String stringValue(Node node) {
int nodeType = node.getNodeType();
if (nodeType == Node.COMMENT_NODE) {
return "";
}
boolean trim = !"preserve".equals(findEnclosingAttribute(node, "xml:space"));
if (nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE) {
String text = node.getNodeValue();
return text == null ? "" : trim ? text.trim() : text;
}
if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) {
String text = ((ProcessingInstruction) node).getData();
return text == null ? "" : trim ? text.trim() : text;
}
NodeList list = node.getChildNodes();
StringBuffer buf = new StringBuffer(16);
for (int i = 0; i < list.getLength(); i++) {
Node child = list.item(i);
buf.append(stringValue(child));
}
return buf.toString();
}
/**
* Locates a node by ID.
*/
public Pointer getPointerByID(JXPathContext context, String id) {
Document document = node.getNodeType() == Node.DOCUMENT_NODE ? (Document) node
: node.getOwnerDocument();
Element element = document.getElementById(id);
return element == null ? (Pointer) new NullPointer(getLocale(), id)
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> setPosition(int position) {
if (position < this.position) {
reset();
}
while (this.position < position) {
if (!nextNode()) {
return false;
}
}
return true;
}
public boolean nextNode() {
if (!setStarted) {
setStarted = true;
currentRootLocation = parentContext.getCurrentNodePointer();
NodePointer parent = currentRootLocation.getParent();
if (parent != null) {
// TBD: check type
stack.push(
parent.childIterator(null, reverse, currentRootLocation));
}
}
while (true) {
if (stack.isEmpty()) {
currentRootLocation = currentRootLocation.getParent();
if (currentRootLocation == null
|| currentRootLocation.isRoot()) {
break;
}
NodePointer parent = currentRootLocation.getParent();
if (parent != null) {
stack.push(
parent.childIterator(
null,
reverse,
currentRootLocation));
}
}
while (!stack.isEmpty()) {
if (!reverse) {
NodeIterator it = (NodeIterator) stack.peek();
if (it.setPosition(it.getPosition() + 1)) {
currentNodePointer = it.getNodePointer();
if (!currentNodePointer.isLeaf()) {
stack.push(
currentNodePointer.childIterator(
null,
reverse,
null));
}
if (currentNodePointer.testNode(nodeTest)) {
super.setPosition(getCurrentPosition() + 1);
return true;
}
}
else {
// We get here only if the name test failed
// and the iterator ended
stack.pop();
}
}
else {
NodeIterator it = (NodeIterator) stack.peek();
if (it.setPosition(it.getPosition() + 1)) {
currentNodePointer = it.getNodePointer();
if (!currentNodePointer.isLeaf()) {
stack.push(
currentNodePointer.childIterator(
null,
reverse,
null));
}
else if (currentNodePointer.testNode(nodeTest)) {
super.setPosition(getCurrentPosition() + 1);
return true;
}
}
else {
stack.pop();
if (!stack.isEmpty()) {
it = (PropertyIterator) stack.peek();
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
currentNodePointer = it.getNodePointer();
if (currentNodePointer.testNode(nodeTest)) {
super.setPosition(getCurrentPosition() + 1);
return true;
}
}
}
}
}
}
return false;
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xffffffff,0xffff0007,0xffff0007,0x0,0x7,};
}
private static void jj_la1_2() {
jj_la1_2 = new int[] {0x7fff,0x4000,0x3fff,0x8000,0x4000,0x14c7fff,0x0,0x0,0x0,0x0,0x1007fff,0x14c7fff,0x100000,0x0,0x0,0x400000,0x10000,0x7fff,0x800000,0x14d7fff,0x0,0x14c7fff,0x100000,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x1000000,0x1000000,0x14d7fff,0x1007fff,0x1007fff,0x8000,0x0,};
}
final private JJCalls[] jj_2_rtns = new JJCalls[6];
private boolean jj_rescan = false;
private int jj_gc = 0;
public XPathParser(java.io.InputStream stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new XPathParserTokenManager(jj_input_stream);
token = new Token();
token.next = jj_nt = token_source.getNextToken();
jj_gen = 0;
for (int i = 0; i < 39; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> }
public void ReInit(java.io.InputStream stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
token.next = jj_nt = token_source.getNextToken();
jj_gen = 0;
for (int i = 0; i < 39; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
public XPathParser(java.io.Reader stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new XPathParserTokenManager(jj_input_stream);
token = new Token();
token.next = jj_nt = token_source.getNextToken();
jj_gen = 0;
for (int i = 0; i < 39; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
public void ReInit(java.io.Reader stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
token.next = jj_nt = token_source.getNextToken();
jj_gen = 0;
for (int i = 0; i < 39; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
public XPathParser(XPathParserTokenManager tm) {
token_source = tm;
token = new Token();
token.next = jj_nt = token_source.getNextToken();
jj_gen = 0;
for (int i = 0; i < 39; i++) jj_la1[i] = -1
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
public void ReInit(XPathParserTokenManager tm) {
token_source = tm;
token = new Token();
token.next = jj_nt = token_source.getNextToken();
jj_gen = 0;
for (int i = 0; i < 39; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
final private Token jj_consume_token(int kind) throws ParseException {
Token oldToken = token;
if ((token = jj_nt).next != null) jj_nt = jj_nt.next;
else jj_nt = jj_nt.next = token_source.getNextToken();
if (token.kind == kind) {
jj_gen++;
if (++jj_gc > 100) {
jj_gc = 0;
for (int i = 0; i < jj_2_rtns.length; i++) {
JJCalls c = jj_2_rtns[i];
while (c != null) {
if (c.gen < jj_gen) c.first = null;
c = c.next;
}
}
}
return token;
}
jj_nt = token;
token = oldToken;
jj_kind = kind;
throw generateParseException();
}
final private boolean jj_scan_token(int kind) {
if (jj_scanpos == jj_lastpos) {
jj_la--;
if (jj_scanpos.next == null) {
jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
} else {
jj_lastpos = jj_scanpos = jj_scanpos.next;
}
} else {
jj_scanpos = jj_scanpos.next;
}
if (jj_rescan) {
int i = 0; Token tok = token;
while (tok != null &&
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> tok != jj_scanpos) { i++; tok = tok.next; }
if (tok != null) jj_add_error_token(kind, i);
}
return (jj_scanpos.kind != kind);
}
final public Token getNextToken() {
if ((token = jj_nt).next != null) jj_nt = jj_nt.next;
else jj_nt = jj_nt.next = token_source.getNextToken();
jj_gen++;
return token;
}
final public Token getToken(int index) {
Token t = lookingAhead ? jj_scanpos : token;
for (int i = 0; i < index; i++) {
if (t.next != null) t = t.next;
else t = t.next = token_source.getNextToken();
}
return t;
}
private java.util.Vector jj_expentries = new java.util.Vector();
private int[] jj_expentry;
private int jj_kind = -1;
private int[] jj_lasttokens = new int[100];
private int jj_endpos;
private void jj_add_error_token(int kind, int pos) {
if (pos >= 100) return;
if (pos == jj_endpos + 1) {
jj_lasttokens[jj_endpos++] = kind;
} else if (jj_endpos != 0) {
jj_expentry = new int[jj_endpos];
for (int i = 0; i < jj_endpos; i++) {
jj_expentry[i] = jj_lasttokens[i];
}
boolean exists = false;
for (java.util.Enumeration en = jj_expentries.elements(); en.hasMoreElements();) {
int[] oldentry = (int[])(en.nextElement());
if (oldentry.length == jj_expentry.length) {
exists = true;
for (int i = 0; i < jj_expentry.length; i++) {
if (oldentry[i] != jj_expentry[i]) {
exists = false;
break;
}
}
if (exists) break;
}
}
if (!exists) jj
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>_token() {
jj_rescan = true;
for (int i = 0; i < 6; i++) {
JJCalls p = jj_2_rtns[i];
do {
if (p.gen > jj_gen) {
jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
switch (i) {
case 0: jj_3_1(); break;
case 1: jj_3_2(); break;
case 2: jj_3_3(); break;
case 3: jj_3_4(); break;
case 4: jj_3_5(); break;
case 5: jj_3_6(); break;
}
}
p = p.next;
} while (p != null);
}
jj_rescan = false;
}
final private void jj_save(int index, int xla) {
JJCalls p = jj_2_rtns[index];
while (p.gen > jj_gen) {
if (p.next == null) { p = p.next = new JJCalls(); break; }
p = p.next;
}
p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
}
static final class JJCalls {
int gen;
Token first;
int arg;
JJCalls next;
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/* Generated By:JavaCC: Do not edit this line. Token.java Version 3.0 */
package org.apache.commons.jxpath.ri.parser;
/**
* Describes the input token stream.
*/
public class Token {
/**
* An integer that describes the kind of this token. This numbering
* system is determined by JavaCCParser, and a table of these numbers is
* stored in the file ...Constants.java.
*/
public int kind;
/**
* beginLine and beginColumn describe the position of the first character
* of this token; endLine and endColumn describe the position of the
* last character of this token.
*/
public int beginLine, beginColumn, endLine, endColumn;
/**
* The string image of the token.
*/
public String image;
/**
* A reference to the next regular (non-special) token from the input
* stream. If this is the last token from the input stream, or if the
* token manager has not read tokens beyond this one, this field is
* set to null. This is true only if this token is also a regular
* token. Otherwise, see below for a description of the contents of
* this field.
*/
public Token next;
/**
* This field is used to access special tokens that occur prior to this
* token, but after the immediately preceding regular (non-special) token.
* If there are no such special tokens, this field is set to null.
* When there are more than one such special token, this field refers
* to the last of these special tokens, which in turn refers to the next
* previous special token through its specialToken field, and so on
* until the first special token (whose specialToken field is null).
* The next fields of special tokens refer to other special tokens that
* immediately follow it (without an intervening regular token). If there
* is no such token, this field is null.
*/
public Token specialToken;
/**
* Returns the image.
*/
public String toString()
{
return image;
}
/**
* Returns a new Token object, by default. However, if you want, you
* can create and return subclass objects based on the value of ofKind.
* Simply add
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.jxpath.functions.ConstructorFunction;
import org.apache.commons.jxpath.functions.MethodFunction;
import org.apache.commons.jxpath.util.MethodLookupUtils;
import org.apache.commons.jxpath.util.TypeUtils;
/**
* Extension functions provided by Java classes. The class prefix specified
* in the constructor is used when a constructor or a static method is called.
* Usually, a class prefix is a package name (hence the name of this class).
*
* Let's say, we declared a PackageFunction like this:
* <blockquote><pre>
* new PackageFunctions("java.util.", "util")
* </pre></blockquote>
*
* We can now use XPaths like:
* <dl>
* <dt><code>"util:Date.new()"</code></dt>
* <dd>Equivalent to <code>new java.util.Date()</code></dd>
* <dt><code>"util:Collections.singleton('foo')"</code></dt>
* <dd>Equivalent to <code>java.util.Collections.singleton("foo")</code></dd>
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>package.Classname.new</b>, if looking for a
* constructor in a subpackage</li>
* <li><b>Classname.methodname</b>, if looking for a static method</li>
* <li><b>subpackage.subpackage.Classname.methodname</b>, if looking for a
* static method of a class in a subpackage</li>
* </ul>
*
* @return a MethodFunction, a ConstructorFunction or null if no function
* is found
*/
public Function getFunction(
String namespace,
String name,
Object[] parameters)
{
if ((namespace == null && this.namespace != null)
|| (namespace != null && !namespace.equals(this.namespace))) {
return null;
}
if (parameters == null) {
parameters = EMPTY_ARRAY;
}
if (parameters.length >= 1) {
Object target = TypeUtils.convert(parameters[0], Object.class);
if (target != null) {
Method method =
MethodLookupUtils.lookupMethod(
target.getClass(),
name,
parameters);
if (method != null) {
return new MethodFunction(method);
}
if (target instanceof NodeSet) {
target = ((NodeSet) target).getPointers();
}
method =
MethodLookupUtils.lookupMethod(
target.getClass(),
name,
parameters);
if (method != null) {
return new MethodFunction(method);
}
if (target instanceof Collection) {
Iterator iter = ((Collection) target).iterator();
if (iter.hasNext()) {
target = iter.next();
if (target instanceof Pointer) {
target = ((Pointer) target).getValue();
}
}
else {
target = null;
}
}
}
if (target != null) {
Method method =
MethodLookupUtils.lookupMethod(
target.getClass(),
name,
parameters);
if (method != null) {
return new MethodFunction(method);
}
}
}
String fullName = classPrefix + name;
int inx = fullName.lastIndexOf('.');
if (inx == -1) {
return null;
}
String className = fullName.substring(0, inx);
String methodName = fullName.substring(inx +
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.Stack;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.Compiler;
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* An EvalContext that walks the "descendant::" and "descendant-or-self::"
* axes.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class DescendantContext extends EvalContext {
private NodeTest nodeTest;
private boolean setStarted = false;
private Stack stack;
private NodePointer currentNodePointer;
private boolean includeSelf;
private static final NodeTest ELEMENT_NODE_TEST =
new NodeTypeTest(Compiler.NODE_TYPE_NODE);
public DescendantContext(
EvalContext parentContext,
boolean includeSelf,
NodeTest nodeTest)
{
super(parentContext);
this.includeSelf = includeSelf;
this.nodeTest = nodeTest;
}
public boolean isChildOrderingRequired() {
return true;
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
public NodePointer getCurrentNodePointer() {
if (position == 0) {
if (!setPosition(1)) {
return null;
}
}
return currentNodePointer;
}
public void reset() {
super.reset();
setStarted = false;
}
public boolean setPosition(int position) {
if (position < this.position) {
reset();
}
while (this.position < position) {
if (!nextNode()) {
return false;
}
}
return true;
}
public boolean nextNode() {
if (!setStarted) {
setStarted = true;
stack = new Stack();
currentNodePointer = parentContext.getCurrentNodePointer();
if (currentNodePointer != null) {
if (!currentNodePointer.isLeaf()) {
stack.push(
currentNodePointer.childIterator(
ELEMENT_NODE_TEST,
false,
null));
}
if (includeSelf) {
if (currentNodePointer.testNode(nodeTest)) {
position++;
return true;
}
}
}
}
while (!stack.isEmpty()) {
NodeIterator it = (NodeIterator) stack.peek();
if (it.setPosition(it.getPosition() + 1)) {
currentNodePointer = it.getNodePointer();
if (!isRecursive()) {
if (!currentNodePointer.isLeaf()) {
stack.push(
currentNodePointer.childIterator(
ELEMENT_NODE_TEST,
false,
null));
}
if (currentNodePointer.testNode(nodeTest)) {
position++;
return true;
}
}
}
else {
// We get here only if the name test failed
// and the iterator ended
stack.pop();
}
}
return false;
}
/**
* Checks if we are reentering a bean we have already seen and if so
* returns true to prevent infinite recursion.
*/
private boolean isRecursive() {
Object node = currentNodePointer.getNode();
for (int i = stack.size() - 1; --i >= 0;) {
NodeIterator it = (NodeIterator) stack.get(i);
Pointer pointer = it.getNodePointer();
if (pointer != null && pointer.getNode() == node) {
return true;
}
}
return false;
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that can walk the "child::", "following-sibling::" and
* "preceding-sibling::" axes.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class ChildContext extends EvalContext {
private NodeTest nodeTest;
private boolean startFromParentLocation;
private boolean reverse;
private NodeIterator iterator;
public ChildContext(
EvalContext parentContext,
NodeTest nodeTest,
boolean startFromParentLocation,
boolean reverse)
{
super(parentContext);
this.nodeTest = nodeTest;
this.startFromParentLocation = startFromParentLocation;
this.reverse = reverse;
}
public NodePointer getCurrentNodePointer() {
if (position == 0) {
if (!setPosition(1)) {
return null;
}
}
return iterator == null ? null : iterator.getNodePointer();
}
/**
* This method is called on the last context on the path when only
* one value is
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> needed. Note that this will return the whole property,
* even if it is a collection. It will not extract the first element
* of the collection. For example, "books" will return the collection
* of books rather than the first book from that collection.
*/
public Pointer getSingleNodePointer() {
if (position == 0) {
while (nextSet()) {
prepare();
if (iterator == null) {
return null;
}
// See if there is a property there, singular or collection
NodePointer pointer = iterator.getNodePointer();
if (pointer != null) {
return pointer;
}
}
return null;
}
return getCurrentNodePointer();
}
public boolean nextNode() {
return setPosition(getCurrentPosition() + 1);
}
public void reset() {
super.reset();
iterator = null;
}
public boolean setPosition(int position) {
int oldPosition = getCurrentPosition();
super.setPosition(position);
if (oldPosition == 0) {
prepare();
}
return iterator == null ? false : iterator.setPosition(position);
}
/**
* Allocates a PropertyIterator.
*/
private void prepare() {
NodePointer parent = parentContext.getCurrentNodePointer();
if (parent == null) {
return;
}
iterator = (startFromParentLocation ? parent.getParent() : parent).childIterator(nodeTest,
reverse, startFromParentLocation ? parent : null);
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.compiler;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.InfoSetUtil;
import org.apache.commons.jxpath.ri.axes.InitialContext;
import org.apache.commons.jxpath.ri.axes.SelfContext;
/**
* Common superclass for the implementations of Expression for the operations
* "=" and "!=".
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public abstract class CoreOperationCompare extends CoreOperation {
private boolean invert;
public CoreOperationCompare(Expression arg1, Expression arg2) {
this(arg1, arg2, false);
}
protected CoreOperationCompare(Expression arg1, Expression arg2, boolean invert) {
super(new Expression[] { arg1, arg2 });
this.invert = invert;
}
public Object computeValue(EvalContext context) {
return equal(context, args[0], args[1]) ? Boolean.TRUE : Boolean.FALSE;
}
protected int getPrecedence() {
return 2;
}
protected boolean isSymmetric() {
return true;
}
/**
* Compares two values
*/
protected boolean equal(
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
EvalContext context,
Expression left,
Expression right)
{
Object l = left.compute(context);
Object r = right.compute(context);
if (l instanceof InitialContext) {
((EvalContext) l).reset();
}
if (l instanceof SelfContext) {
l = ((EvalContext) l).getSingleNodePointer();
}
if (r instanceof InitialContext) {
((EvalContext) r).reset();
}
if (r instanceof SelfContext) {
r = ((EvalContext) r).getSingleNodePointer();
}
if (l instanceof Collection) {
l = ((Collection) l).iterator();
}
if (r instanceof Collection) {
r = ((Collection) r).iterator();
}
if (l instanceof Iterator && r instanceof Iterator) {
return findMatch((Iterator) l, (Iterator) r);
}
if (l instanceof Iterator) {
return contains((Iterator) l, r);
}
if (r instanceof Iterator) {
return contains((Iterator) r, l);
}
return equal(l, r);
}
protected boolean contains(Iterator it, Object value) {
while (it.hasNext()) {
Object element = it.next();
if (equal(element, value)) {
return true;
}
}
return false;
}
protected boolean findMatch(Iterator lit, Iterator rit) {
HashSet left = new HashSet();
while (lit.hasNext()) {
left.add(lit.next());
}
while (rit.hasNext()) {
if (contains(left.iterator(), rit.next())) {
return true;
}
}
return false;
}
protected boolean equal(Object l, Object r) {
if (l instanceof Pointer) {
l = ((Pointer) l).getValue();
}
if (r instanceof Pointer) {
r = ((Pointer) r).getValue();
}
boolean result;
if (l instanceof Boolean || r instanceof Boolean) {
result = l == r || InfoSetUtil.booleanValue(l) == InfoSetUtil.booleanValue(r);
} else if (l instanceof Number || r instanceof Number) {
//if either side is NaN, no comparison returns true:
double ld = InfoSetUtil.doubleValue(l);
if (Double
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.compiler;
import org.apache.commons.jxpath.NodeSet;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.util.ValueUtils;
import java.util.Collections;
import java.util.Iterator;
import java.util.Locale;
/**
* Common superclass for several types of nodes in the parse tree. Provides
* APIs for optimization of evaluation of expressions. Specifically, an
* expression only needs to executed once during the evaluation of an xpath
* if that expression is context-independent. Expression.isContextDependent()
* provides that hint.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public abstract class Expression {
protected static final Double ZERO = new Double(0);
protected static final Double ONE = new Double(1);
protected static final Double NOT_A_NUMBER = new Double(Double.NaN);
private boolean contextDependencyKnown = false;
private boolean contextDependent;
/**
* Returns true if this expression should be re-evaluated
* each time the current position in the context changes.
*/
public boolean isContextDependent() {
if (!context
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>DependencyKnown) {
contextDependent = computeContextDependent();
contextDependencyKnown = true;
}
return contextDependent;
}
/**
* Implemented by subclasses and result is cached by isContextDependent()
*/
public abstract boolean computeContextDependent();
/**
* Evaluates the expression. If the result is a node set, returns
* the first element of the node set.
*/
public abstract Object computeValue(EvalContext context);
public abstract Object compute(EvalContext context);
public Iterator iterate(EvalContext context) {
Object result = compute(context);
if (result instanceof EvalContext) {
return new ValueIterator((EvalContext) result);
}
if (result instanceof NodeSet) {
return new ValueIterator(((NodeSet) result).getPointers().iterator());
}
return ValueUtils.iterate(result);
}
public Iterator iteratePointers(EvalContext context) {
Object result = compute(context);
if (result == null) {
return Collections.EMPTY_LIST.iterator();
}
if (result instanceof EvalContext) {
return (EvalContext) result;
}
if (result instanceof NodeSet) {
return new PointerIterator(((NodeSet) result).getPointers().iterator(),
new QName(null, "value"),
context.getRootContext().getCurrentNodePointer().getLocale());
}
return new PointerIterator(ValueUtils.iterate(result),
new QName(null, "value"),
context.getRootContext().getCurrentNodePointer().getLocale());
}
public static class PointerIterator implements Iterator {
private Iterator iterator;
private QName qname;
private Locale locale;
//to what method does the following comment refer?
/**
* @deprecated Use the method that takes a NamespaceManager
*/
public PointerIterator(Iterator it, QName qname, Locale locale) {
this.iterator = it;
this.qname = qname;
this.locale = locale;
}
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
Object o = iterator.next();
return o instanceof Pointer ? o : NodePointer.newNodePointer(qname, o, locale);
}
public void remove() {
throw new UnsupportedOperationException();
}
}
public static class ValueIterator implements Iterator {
private Iterator iterator;
public ValueIterator(Iterator it) {
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
this.iterator = it;
}
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
Object o = iterator.next();
return o instanceof Pointer ? ((Pointer) o).getValue() : o;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> object has been created due to a parse error, the token
* followng this token will (therefore) be the first error token.
*/
public Token currentToken;
/**
* Each entry in this array is an array of integers. Each array
* of integers represents a sequence of tokens (by their ordinal
* values) that is expected at this point of the parse.
*/
public int[][] expectedTokenSequences;
/**
* This is a reference to the "tokenImage" array of the generated
* parser within which the parse error occurred. This array is
* defined in the generated ...Constants interface.
*/
public String[] tokenImage;
/**
* This method has the standard behavior when this object has been
* created using the standard constructors. Otherwise, it uses
* "currentToken" and "expectedTokenSequences" to generate a parse
* error message and returns it. If this object has been created
* due to a parse error, and you do not catch it (it gets thrown
* from the parser), then this method is called during the printing
* of the final stack trace, and hence the correct error message
* gets displayed.
*/
public String getMessage() {
if (!specialConstructor) {
return super.getMessage();
}
String expected = "";
int maxSize = 0;
for (int i = 0; i < expectedTokenSequences.length; i++) {
if (maxSize < expectedTokenSequences[i].length) {
maxSize = expectedTokenSequences[i].length;
}
for (int j = 0; j < expectedTokenSequences[i].length; j++) {
expected += tokenImage[expectedTokenSequences[i][j]] + " ";
}
if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
expected += "...";
}
expected += eol + " ";
}
String retval = "Encountered \"";
Token tok = currentToken.next;
for (int i = 0; i < maxSize; i++) {
if (i != 0) retval += " ";
if (tok.kind == 0) {
retval += tokenImage[0];
break;
}
retval += add_escapes(tok.image);
tok = tok.next;
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
}
retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
retval += "." + eol;
if (expectedTokenSequences.length == 1) {
retval += "Was expecting:" + eol + " ";
} else {
retval += "Was expecting one of:" + eol + " ";
}
retval += expected;
return retval;
}
/**
* The end of line string for this machine.
*/
protected String eol = System.getProperty("line.separator", "\n");
/**
* Used to convert raw characters to their escaped version
* when these raw version cannot be used as part of an ASCII
* string literal.
*/
protected String add_escapes(String str) {
StringBuffer retval = new StringBuffer();
char ch;
for (int i = 0; i < str.length(); i++) {
switch (str.charAt(i))
{
case 0 :
continue;
case '\b':
retval.append("\\b");
continue;
case '\t':
retval.append("\\t");
continue;
case '\n':
retval.append("\\n");
continue;
case '\f':
retval.append("\\f");
continue;
case '\r':
retval.append("\\r");
continue;
case '\"':
retval.append("\\\"");
continue;
case '\'':
retval.append("\\\'");
continue;
case '\\':
retval.append("\\\\");
continue;
default:
if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
String s = "0000" + Integer.toString(ch, 16);
retval.append("\\u" + s.substring(s.length() - 4, s.length()));
} else {
retval.append(ch);
}
continue;
}
}
return retval.toString();
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.apache.commons.jxpath.ri.model.VariablePointer;
/**
* Type conversions, XPath style.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class InfoSetUtil {
private static final Double ZERO = new Double(0);
private static final Double ONE = new Double(1);
private static final Double NOT_A_NUMBER = new Double(Double.NaN);
/**
* Converts the supplied object to String
*/
public static String stringValue(Object object) {
if (object instanceof String) {
return (String) object;
}
if (object instanceof Number) {
double d = ((Number) object).doubleValue();
long l = ((Number) object).longValue();
return d == l ? String.valueOf(l) : String.valueOf(d);
}
if (object instanceof Boolean) {
return ((Boolean) object).booleanValue() ? "true" : "false";
}
if (object == null) {
return "";
}
if (object instanceof NodePointer) {
return stringValue(((NodePointer) object).getValue());
}
if (object instanceof EvalContext) {
EvalContext ctx = (EvalContext) object;
Pointer ptr = ctx.getSingle
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>NodePointer();
return ptr == null ? "" : stringValue(ptr);
}
return String.valueOf(object);
}
/**
* Converts the supplied object to Number
*/
public static Number number(Object object) {
if (object instanceof Number) {
return (Number) object;
}
if (object instanceof Boolean) {
return ((Boolean) object).booleanValue() ? ONE : ZERO;
}
if (object instanceof String) {
try {
return new Double((String) object);
}
catch (NumberFormatException ex) {
return NOT_A_NUMBER;
}
}
if (object instanceof EvalContext) {
EvalContext ctx = (EvalContext) object;
Pointer ptr = ctx.getSingleNodePointer();
return ptr == null ? NOT_A_NUMBER : number(ptr);
}
if (object instanceof NodePointer) {
return number(((NodePointer) object).getValue());
}
return number(stringValue(object));
}
/**
* Converts the supplied object to double
*/
public static double doubleValue(Object object) {
if (object instanceof Number) {
return ((Number) object).doubleValue();
}
if (object instanceof Boolean) {
return ((Boolean) object).booleanValue() ? 0.0 : 1.0;
}
if (object instanceof String) {
if (object.equals("")) {
return 0.0;
}
try {
return Double.parseDouble((String) object);
}
catch (NumberFormatException ex) {
return Double.NaN;
}
}
if (object instanceof NodePointer) {
return doubleValue(((NodePointer) object).getValue());
}
if (object instanceof EvalContext) {
EvalContext ctx = (EvalContext) object;
Pointer ptr = ctx.getSingleNodePointer();
return ptr == null ? Double.NaN : doubleValue(ptr);
}
return doubleValue(stringValue(object));
}
/**
* Converts the supplied object to boolean
*/
public static boolean booleanValue(Object object) {
if (object instanceof Number) {
double value = ((Number) object).doubleValue();
return value != 0 && value != -0 && !Double.isNaN(value);
}
if (object instanceof Boolean) {
return ((Boolean) object).booleanValue();
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
}
if (object instanceof EvalContext) {
EvalContext ctx = (EvalContext) object;
Pointer ptr = ctx.getSingleNodePointer();
return ptr == null ? false : booleanValue(ptr);
}
if (object instanceof String) {
return ((String) object).length() != 0;
}
if (object instanceof NodePointer) {
NodePointer pointer = (NodePointer) object;
if (pointer instanceof VariablePointer) {
return booleanValue(pointer.getNode());
}
pointer = pointer.getValuePointer();
return pointer.isActual();
}
return object != null;
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* A single-set EvalContext that provides access to the current node of
* the parent context and nothing else. It does not pass the iteration
* on to the parent context.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class InitialContext extends EvalContext {
private boolean started = false;
private boolean collection;
private NodePointer nodePointer;
public InitialContext(EvalContext parentContext) {
super(parentContext);
nodePointer =
(NodePointer) parentContext.getCurrentNodePointer().clone();
if (nodePointer != null) {
collection =
(nodePointer.getIndex() == NodePointer.WHOLE_COLLECTION);
}
}
public Pointer getSingleNodePointer() {
return nodePointer;
}
public NodePointer getCurrentNodePointer() {
return nodePointer;
}
public Object getValue() {
return nodePointer.getValue();
}
public boolean nextNode() {
return setPosition(position + 1);
}
public boolean setPosition(int position) {
this.position = position;
if (collection) {
if (position >= 1 && position <= nodePointer.getLength())
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath;
import java.util.Iterator;
/**
* Represents a compiled XPath. The interpretation of compiled XPaths
* may be faster, because it bypasses the compilation step. The reference
* implementation of JXPathContext also globally caches some of the
* results of compilation, so the direct use of JXPathContext is not
* always less efficient than the use of CompiledExpression.
* <p>
* Use CompiledExpression only when there is a need to evaluate the
* same expression multiple times and the CompiledExpression can be
* conveniently cached.
* <p>
* To acqure a CompiledExpression, call {@link JXPathContext#compile
* JXPathContext.compile}
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public interface CompiledExpression {
/**
* Evaluates the xpath and returns the resulting object. Primitive
* types are wrapped into objects.
*/
Object getValue(JXPathContext context);
/**
* Evaluates the xpath, converts the result to the specified class and
* returns the resulting object.
*/
Object getValue(JXPathContext context, Class requiredType);
/**
* Modifies the value of the property described by the supplied xpath.
* Will throw an exception if one of the following conditions occurs:
* <ul>
* <li>The xpath does not in fact describe an existing property
* <li>The property is not writable
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> (no public, non-static set method)
* </ul>
*/
void setValue(JXPathContext context, Object value);
/**
* Creates intermediate elements of
* the path by invoking an AbstractFactory, which should first be
* installed on the context by calling "setFactory".
*/
Pointer createPath(JXPathContext context);
/**
* The same as setValue, except it creates intermediate elements of
* the path by invoking an AbstractFactory, which should first be
* installed on the context by calling "setFactory".
* <p>
* Will throw an exception if one of the following conditions occurs:
* <ul>
* <li>Elements of the xpath aleady exist, by the path does not in
* fact describe an existing property
* <li>The AbstractFactory fails to create an instance for an intermediate
* element.
* <li>The property is not writable (no public, non-static set method)
* </ul>
*/
Pointer createPathAndSetValue(JXPathContext context, Object value);
/**
* Traverses the xpath and returns a Iterator of all results found
* for the path. If the xpath matches no properties
* in the graph, the Iterator will not be null.
*/
Iterator iterate(JXPathContext context);
/**
* Traverses the xpath and returns a Pointer.
* A Pointer provides easy access to a property.
* If the xpath matches no properties
* in the graph, the pointer will be null.
*/
Pointer getPointer(JXPathContext context, String xpath);
/**
* Traverses the xpath and returns an Iterator of Pointers.
* A Pointer provides easy access to a property.
* If the xpath matches no properties
* in the graph, the Iterator be empty, but not null.
*/
Iterator iteratePointers(JXPathContext context);
/**
* Remove the graph element described by this expression
*/
void removePath(JXPathContext context);
/**
* Remove all graph elements described by this expression
*/
void removeAll(JXPathContext context);
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
return bestMatch;
}
// This step did not find anything - return a null pointer
return createNullPointer(context, childPointer, steps, currentStep);
}
// Evaluate subsequent steps
return doStep(context, childPointer, steps, currentStep + 1);
}
/**
* A path that starts with a standard InfoSet node (e.g. DOM Node) and
* has no predicates. Get a child iterator and apply the tail of
* the path to each element until we find a match. If we don't find
* a perfect match, we should return the "best quality" pointer, which
* has the longest chain of steps mapping to existing nodes and the shortes
* tail of Null* pointers.
*/
private static NodePointer doStepNoPredicatesStandard(
EvalContext context, NodePointer parentPointer,
Step[] steps, int currentStep)
{
Step step = steps[currentStep];
if (step.getAxis() == Compiler.AXIS_SELF) {
return doStep(context, parentPointer, steps, currentStep + 1);
}
int bestQuality = 0;
NodePointer bestMatch = null;
NodeIterator it = getNodeIterator(context, parentPointer, step);
if (it != null) {
for (int i = 1; it.setPosition(i); i++) {
NodePointer childPointer = it.getNodePointer();
if (steps.length == currentStep + 1) {
// If this is the last step - we are done, we found it
return childPointer;
}
NodePointer pointer = doStep(
context, childPointer, steps, currentStep + 1);
int quality = computeQuality(pointer);
if (quality == PERFECT_MATCH) {
return pointer;
}
if (quality > bestQuality) {
bestQuality = quality;
bestMatch = (NodePointer) pointer.clone();
}
}
}
return bestMatch != null ? bestMatch
: createNullPointer(context, parentPointer, steps, currentStep);
}
/**
* A path that starts with a property owner. The method evaluates
* the first predicate in a special way and then forwards to
* a general predicate processing method.
*/
private static NodePointer doStepPredicatesPropertyOwner(
EvalContext
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
if (predicates.length == 1) {
NodeIterator it = getNodeIterator(context, parent, step);
NodePointer pointer = null;
if (it != null) {
if (predicate instanceof NameAttributeTest) { // [@name = key]
String key = keyFromPredicate(context, predicate);
for (int i = 1; it.setPosition(i); i++) {
NodePointer ptr = it.getNodePointer();
if (isNameAttributeEqual(ptr, key)) {
pointer = ptr;
break;
}
}
}
else {
int index = indexFromPredicate(context, predicate);
if (it.setPosition(index + 1)) {
pointer = it.getNodePointer();
}
}
}
if (pointer != null) {
return doStep(context, pointer, steps, currentStep + 1);
}
}
else {
NodeIterator it = getNodeIterator(context, parent, step);
if (it != null) {
List list = new ArrayList();
for (int i = 1; it.setPosition(i); i++) {
list.add(it.getNodePointer());
}
NodePointer pointer =
doPredicatesStandard(
context,
list,
steps,
currentStep,
predicates,
0);
if (pointer != null) {
return pointer;
}
}
}
return createNullPointer(context, parent, steps, currentStep);
}
/**
* Evaluates predicates and proceeds with the subsequent steps
* of the path.
*/
private static NodePointer doPredicate(
EvalContext context, NodePointer parent,
Step[] steps, int currentStep,
Expression predicates[], int currentPredicate)
{
if (currentPredicate == predicates.length) {
return doStep(context, parent, steps, currentStep + 1);
}
Expression predicate = predicates[currentPredicate];
if (predicate instanceof NameAttributeTest) { // [@name = key1]
return doPredicateName(
context,
parent,
steps,
currentStep,
predicates,
currentPredicate);
}
// else [index]
return doPredicateIndex(
context,
parent,
steps,
currentStep,
predicates,
currentPredicate);
}
private static NodePointer doPredicateName(
EvalContext
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> * For a pointer that matches an actual node, returns 0.
* For a pointer that does not match an actual node, but whose
* parent pointer does returns -1, etc.
*/
private static int computeQuality(NodePointer pointer) {
int quality = PERFECT_MATCH;
while (pointer != null && !pointer.isActual()) {
quality--;
pointer = pointer.getImmediateParentPointer();
}
return quality;
}
/**
* Returns true if the pointer has an attribute called "name" and
* its value is equal to the supplied string.
*/
private static boolean isNameAttributeEqual(
NodePointer pointer,
String name)
{
NodeIterator it = pointer.attributeIterator(QNAME_NAME);
return it != null
&& it.setPosition(1)
&& name.equals(it.getNodePointer().getValue());
}
/**
* Returns true if the pointer is a collection and the index is
* withing the bounds of the collection.
*/
private static boolean isCollectionElement(
NodePointer pointer,
int index)
{
return pointer.isActual()
&& (index == 0
|| (pointer.isCollection()
&& index >= 0
&& index < pointer.getLength()));
}
/**
* For an intermediate pointer (e.g. PropertyPointer, ContainerPointer)
* returns a pointer for the contained value.
*/
private static NodePointer valuePointer(NodePointer pointer) {
return pointer == null ? null : pointer.getValuePointer();
}
/**
* Creates a "null pointer" that
* a) represents the requested path and
* b) can be used for creation of missing nodes in the path.
*/
public static NodePointer createNullPointer(
EvalContext context, NodePointer parent, Step[] steps,
int currentStep)
{
if (currentStep == steps.length) {
return parent;
}
parent = valuePointer(parent);
Step step = steps[currentStep];
int axis = step.getAxis();
if (axis == Compiler.AXIS_CHILD || axis == Compiler.AXIS_ATTRIBUTE) {
NullPropertyPointer pointer = new NullPropertyPointer(parent);
QName name = ((NodeNameTest) step.getNodeTest()).getNodeName();
pointer.setPropertyName(name.toString());
pointer.setAttribute(
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.model.jdom;
import java.util.List;
import org.apache.commons.jxpath.AbstractFactory;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
import org.jdom.Element;
/**
* Test AbstractFactory.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class TestJDOMFactory extends AbstractFactory {
/**
* Create a new instance and put it in the collection on the parent object.
* Return <b>false</b> if this factory cannot create the requested object.
*/
public boolean createObject(
JXPathContext context,
Pointer pointer,
Object parent,
String name,
int index)
{
if (name.equals("location")
|| name.equals("address")
|| name.equals("street")) {
addJDOMElement((Element) parent, index, name, null);
return true;
}
if (name.startsWith("price:")) {
String namespaceURI = context.getNamespaceURI("price");
addJDOMElement((Element) parent, index, name, namespaceURI);
return true;
}
return false;
}
private void addJDOMElement(Element parent, int index, String tag, String namespaceURI) {
List children = parent.getContent();
int count = 0;
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.Iterator;
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.InfoSetUtil;
import org.apache.commons.jxpath.ri.compiler.Expression;
import org.apache.commons.jxpath.ri.compiler.NameAttributeTest;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
/**
* EvalContext that checks predicates.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class PredicateContext extends EvalContext {
private Expression expression;
private boolean done = false;
private Expression nameTestExpression;
private PropertyPointer dynamicPropertyPointer;
public PredicateContext(EvalContext parentContext, Expression expression) {
super(parentContext);
this.expression = expression;
if (expression instanceof NameAttributeTest) {
nameTestExpression =
((NameAttributeTest) expression).getNameTestExpression();
}
}
public boolean nextNode() {
if (done) {
return false;
}
while (parentContext.nextNode()) {
if (setupDynamicPropertyPointer()) {
Object pred = nameTestExpression.computeValue(
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>parentContext);
String propertyName = InfoSetUtil.stringValue(pred);
// At this point it would be nice to say:
// dynamicPropertyPointer.setPropertyName(propertyName)
// and then: dynamicPropertyPointer.isActual().
// However some PropertyPointers, e.g. DynamicPropertyPointer
// will declare that any property you ask for is actual.
// That's not acceptable for us: we really need to know
// if the property is currently declared. Thus,
// we'll need to perform a search.
boolean ok = false;
String names[] = dynamicPropertyPointer.getPropertyNames();
for (int i = 0; i < names.length; i++) {
if (names[i].equals(propertyName)) {
ok = true;
break;
}
}
if (ok) {
dynamicPropertyPointer.setPropertyName(propertyName);
position++;
return true;
}
}
else {
Object pred = expression.computeValue(parentContext);
if (pred instanceof Iterator) {
if (!((Iterator) pred).hasNext()) {
return false;
}
pred = ((Iterator) pred).next();
}
if (pred instanceof NodePointer) {
pred = ((NodePointer) pred).getNode();
}
if (pred instanceof Number) {
int pos = (int) InfoSetUtil.doubleValue(pred);
position++;
done = true;
return parentContext.setPosition(pos);
}
if (InfoSetUtil.booleanValue(pred)) {
position++;
return true;
}
}
}
return false;
}
/**
* Used for an optimized access to dynamic properties using the
* "map[@name = 'name']" syntax
*/
private boolean setupDynamicPropertyPointer() {
if (nameTestExpression == null) {
return false;
}
NodePointer parent = parentContext.getCurrentNodePointer();
if (parent == null) {
return false;
}
parent = parent.getValuePointer();
if (!(parent instanceof PropertyOwnerPointer)) {
return false;
}
dynamicPropertyPointer =
(PropertyPointer) ((PropertyOwnerPointer) parent)
.getPropertyPointer()
.clone();
return true;
}
public boolean setPosition(int position) {
if (nameTestExpression == null) {
return setPositionStandard(position
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.util;
import java.beans.IndexedPropertyDescriptor;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.jxpath.Container;
import org.apache.commons.jxpath.DynamicPropertyHandler;
import org.apache.commons.jxpath.JXPathException;
/**
* Collection and property access utilities.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class ValueUtils {
private static Map dynamicPropertyHandlerMap = new HashMap();
private static final int UNKNOWN_LENGTH_MAX_COUNT = 16000;
/**
* Returns true if the object is an array or a Collection
*/
public static boolean isCollection(Object value) {
if (value == null) {
return false;
}
value = getValue(value);
if (value.getClass().isArray()) {
return true;
}
if (value instanceof Collection) {
return true;
}
return false;
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
}
return 1;
}
/**
* Returns an iterator for the supplied collection. If the argument
* is null, returns an empty iterator. If the argument is not
* a collection, returns an iterator that produces just that one object.
*/
public static Iterator iterate(Object collection) {
if (collection == null) {
return Collections.EMPTY_LIST.iterator();
}
if (collection.getClass().isArray()) {
int length = Array.getLength(collection);
if (length == 0) {
return Collections.EMPTY_LIST.iterator();
}
ArrayList list = new ArrayList();
for (int i = 0; i < length; i++) {
list.add(Array.get(collection, i));
}
return list.iterator();
}
if (collection instanceof Collection) {
return ((Collection) collection).iterator();
}
return Collections.singletonList(collection).iterator();
}
/**
* Grows the collection if necessary to the specified size. Returns
* the new, expanded collection.
*/
public static Object expandCollection(Object collection, int size) {
if (collection == null) {
return null;
}
if (collection.getClass().isArray()) {
Object bigger =
Array.newInstance(
collection.getClass().getComponentType(),
size);
System.arraycopy(
collection,
0,
bigger,
0,
Array.getLength(collection));
return bigger;
}
if (collection instanceof Collection) {
while (((Collection) collection).size() < size) {
((Collection) collection).add(null);
}
return collection;
}
throw new JXPathException(
"Cannot turn "
+ collection.getClass().getName()
+ " into a collection of size "
+ size);
}
/**
* Returns the index'th element from the supplied collection.
*/
public static Object remove(Object collection, int index) {
collection = getValue(collection);
if (collection == null) {
return null;
}
if (collection.getClass().isArray()) {
int length = Array.getLength(collection);
Object smaller =
Array.newInstance(
collection.getClass().getComponentType(),
length - 1);
if (index > 0) {
System.arraycopy(collection, 0, smaller, 0
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>, index);
}
if (index < length - 1) {
System.arraycopy(
collection,
index + 1,
smaller,
index,
length - index - 1);
}
return smaller;
}
if (collection instanceof List) {
int size = ((List) collection).size();
if (index < size) {
((List) collection).remove(index);
}
return collection;
}
if (collection instanceof Collection) {
Iterator it = ((Collection) collection).iterator();
for (int i = 0; i < index; i++) {
if (!it.hasNext()) {
break;
}
it.next();
}
if (it.hasNext()) {
it.next();
it.remove();
}
return collection;
}
throw new JXPathException(
"Cannot remove "
+ collection.getClass().getName()
+ "["
+ index
+ "]");
}
/**
* Returns the index'th element of the supplied collection.
*/
public static Object getValue(Object collection, int index) {
collection = getValue(collection);
Object value = collection;
if (collection != null) {
if (collection.getClass().isArray()) {
if (index < 0 || index >= Array.getLength(collection)) {
return null;
}
value = Array.get(collection, index);
}
else if (collection instanceof List) {
if (index < 0 || index >= ((List) collection).size()) {
return null;
}
value = ((List) collection).get(index);
}
else if (collection instanceof Collection) {
int i = 0;
Iterator it = ((Collection) collection).iterator();
for (; i < index; i++) {
it.next();
}
if (it.hasNext()) {
value = it.next();
}
else {
value = null;
}
}
}
return value;
}
/**
* Modifies the index'th element of the supplied collection.
* Converts the value to the required type if necessary.
*/
public static void setValue(Object collection, int index, Object value) {
collection = getValue(collection);
if (collection != null) {
if (collection.getClass().isArray()) {
Array.
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import java.util.Map.Entry;
import org.apache.commons.jxpath.CompiledExpression;
import org.apache.commons.jxpath.Function;
import org.apache.commons.jxpath.Functions;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.JXPathFunctionNotFoundException;
import org.apache.commons.jxpath.JXPathInvalidSyntaxException;
import org.apache.commons.jxpath.JXPathNotFoundException;
import org.apache.commons.jxpath.JXPathTypeConversionException;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.axes.InitialContext;
import org.apache.commons.jxpath.ri.axes.RootContext;
import org.apache.commons.jxpath.ri.compiler.Expression;
import org.apache.commons.jxpath.ri.compiler.LocationPath;
import org.apache.commons.jxpath.ri.
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>.JDOMPointerFactory",
"org.jdom.Document");
if (jdomFactory != null) {
nodeFactories.add(jdomFactory);
}
// DynaBean factory is only registered if BeanUtils are on the classpath
Object dynaBeanFactory =
allocateConditionally(
"org.apache.commons.jxpath.ri.model.dynabeans."
+ "DynaBeanPointerFactory",
"org.apache.commons.beanutils.DynaBean");
if (dynaBeanFactory != null) {
nodeFactories.add(dynaBeanFactory);
}
nodeFactories.add(new ContainerPointerFactory());
createNodeFactoryArray();
}
private Pointer rootPointer;
private Pointer contextPointer;
protected NamespaceResolver namespaceResolver;
// The frequency of the cache cleanup
private static final int CLEANUP_THRESHOLD = 500;
protected JXPathContextReferenceImpl(JXPathContext parentContext,
Object contextBean)
{
this(parentContext, contextBean, null);
}
public JXPathContextReferenceImpl(
JXPathContext parentContext,
Object contextBean,
Pointer contextPointer)
{
super(parentContext, contextBean);
synchronized (nodeFactories) {
createNodeFactoryArray();
}
if (contextPointer != null) {
this.contextPointer = contextPointer;
this.rootPointer =
NodePointer.newNodePointer(
new QName(null, "root"),
contextPointer.getRootNode(),
getLocale());
}
else {
this.contextPointer =
NodePointer.newNodePointer(
new QName(null, "root"),
contextBean,
getLocale());
this.rootPointer = this.contextPointer;
}
NamespaceResolver parentNR = null;
if (parentContext instanceof JXPathContextReferenceImpl) {
parentNR = ((JXPathContextReferenceImpl) parentContext).getNamespaceResolver();
}
namespaceResolver = new NamespaceResolver(parentNR);
namespaceResolver
.setNamespaceContextPointer((NodePointer) this.contextPointer);
}
private static void createNodeFactoryArray() {
if (nodeFactoryArray == null) {
nodeFactoryArray =
(NodePointerFactory[]) nodeFactories.
toArray(new NodePointerFactory[nodeFactories.size()]);
Arrays.sort(nodeFactoryArray, new
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> Comparator() {
public int compare(Object a, Object b) {
int orderA = ((NodePointerFactory) a).getOrder();
int orderB = ((NodePointerFactory) b).getOrder();
return orderA - orderB;
}
});
}
}
/**
* Call this with a custom NodePointerFactory to add support for
* additional types of objects. Make sure the factory returns
* a name that puts it in the right position on the list of factories.
*/
public static void addNodePointerFactory(NodePointerFactory factory) {
synchronized (nodeFactories) {
nodeFactories.add(factory);
nodeFactoryArray = null;
}
}
public static NodePointerFactory[] getNodePointerFactories() {
return nodeFactoryArray;
}
/**
* Returns a static instance of TreeCompiler.
*
* Override this to return an aternate compiler.
*/
protected Compiler getCompiler() {
return COMPILER;
}
protected CompiledExpression compilePath(String xpath) {
return new JXPathCompiledExpression(xpath, compileExpression(xpath));
}
private Expression compileExpression(String xpath) {
Expression expr;
synchronized (compiled) {
if (USE_SOFT_CACHE) {
expr = null;
SoftReference ref = (SoftReference) compiled.get(xpath);
if (ref != null) {
expr = (Expression) ref.get();
}
}
else {
expr = (Expression) compiled.get(xpath);
}
}
if (expr != null) {
return expr;
}
expr = (Expression) Parser.parseExpression(xpath, getCompiler());
synchronized (compiled) {
if (USE_SOFT_CACHE) {
if (cleanupCount++ >= CLEANUP_THRESHOLD) {
Iterator it = compiled.entrySet().iterator();
while (it.hasNext()) {
Entry me = (Entry) it.next();
if (((SoftReference) me.getValue()).get() == null) {
it.remove();
}
}
cleanupCount = 0;
}
compiled.put(xpath, new SoftReference(expr));
}
else {
compiled.put(xpath, expr);
}
}
return expr;
}
/**
* Traverses the xpath and returns the resulting object. Primitive
* types
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> requiredType)) {
throw new JXPathTypeConversionException(
"Invalid expression type. '"
+ xpath
+ "' returns "
+ value.getClass().getName()
+ ". It cannot be converted to "
+ requiredType.getName());
}
value = TypeUtils.convert(value, requiredType);
}
return value;
}
/**
* Traverses the xpath and returns a Iterator of all results found
* for the path. If the xpath matches no properties
* in the graph, the Iterator will not be null.
*/
public Iterator iterate(String xpath) {
return iterate(xpath, compileExpression(xpath));
}
public Iterator iterate(String xpath, Expression expr) {
return expr.iterate(getEvalContext());
}
public Pointer getPointer(String xpath) {
return getPointer(xpath, compileExpression(xpath));
}
public Pointer getPointer(String xpath, Expression expr) {
Object result = expr.computeValue(getEvalContext());
if (result instanceof EvalContext) {
result = ((EvalContext) result).getSingleNodePointer();
}
if (result instanceof Pointer) {
if (!isLenient() && !((NodePointer) result).isActual()) {
throw new JXPathNotFoundException("No pointer for xpath: "
+ xpath);
}
return (Pointer) result;
}
return NodePointer.newNodePointer(null, result, getLocale());
}
public void setValue(String xpath, Object value) {
setValue(xpath, compileExpression(xpath), value);
}
public void setValue(String xpath, Expression expr, Object value) {
try {
setValue(xpath, expr, value, false);
}
catch (Throwable ex) {
throw new JXPathException(
"Exception trying to set value with xpath " + xpath, ex);
}
}
public Pointer createPath(String xpath) {
return createPath(xpath, compileExpression(xpath));
}
public Pointer createPath(String xpath, Expression expr) {
try {
Object result = expr.computeValue(getEvalContext());
Pointer pointer = null;
if (result instanceof Pointer) {
pointer = (Pointer) result;
}
else if (result instanceof EvalContext) {
EvalContext ctx = (EvalContext) result;
pointer = ctx.getSingleNode
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>Pointer();
}
else {
checkSimplePath(expr);
// This should never happen
throw new JXPathException("Cannot create path:" + xpath);
}
return ((NodePointer) pointer).createPath(this);
}
catch (Throwable ex) {
throw new JXPathException(
"Exception trying to create xpath " + xpath,
ex);
}
}
public Pointer createPathAndSetValue(String xpath, Object value) {
return createPathAndSetValue(xpath, compileExpression(xpath), value);
}
public Pointer createPathAndSetValue(
String xpath,
Expression expr,
Object value)
{
try {
return setValue(xpath, expr, value, true);
}
catch (Throwable ex) {
throw new JXPathException(
"Exception trying to create xpath " + xpath,
ex);
}
}
private Pointer setValue(
String xpath,
Expression expr,
Object value,
boolean create)
{
Object result = expr.computeValue(getEvalContext());
Pointer pointer = null;
if (result instanceof Pointer) {
pointer = (Pointer) result;
}
else if (result instanceof EvalContext) {
EvalContext ctx = (EvalContext) result;
pointer = ctx.getSingleNodePointer();
}
else {
if (create) {
checkSimplePath(expr);
}
// This should never happen
throw new JXPathException("Cannot set value for xpath: " + xpath);
}
if (create) {
pointer = ((NodePointer) pointer).createPath(this, value);
}
else {
pointer.setValue(value);
}
return pointer;
}
/**
* Checks if the path follows the JXPath restrictions on the type
* of path that can be passed to create... methods.
*/
private void checkSimplePath(Expression expr) {
if (!(expr instanceof LocationPath)
|| !((LocationPath) expr).isSimplePath()) {
throw new JXPathInvalidSyntaxException(
"JXPath can only create a path if it uses exclusively "
+ "the child:: and attribute:: axes and has "
+ "no context-dependent predicates");
}
}
/**
* Traverses the xpath and returns an Iterator of Pointers.
* A Pointer provides easy access to a property.
*
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> If the xpath matches no properties
* in the graph, the Iterator be empty, but not null.
*/
public Iterator iteratePointers(String xpath) {
return iteratePointers(xpath, compileExpression(xpath));
}
public Iterator iteratePointers(String xpath, Expression expr) {
return expr.iteratePointers(getEvalContext());
}
public void removePath(String xpath) {
removePath(xpath, compileExpression(xpath));
}
public void removePath(String xpath, Expression expr) {
try {
NodePointer pointer = (NodePointer) getPointer(xpath, expr);
if (pointer != null) {
((NodePointer) pointer).remove();
}
}
catch (Throwable ex) {
throw new JXPathException(
"Exception trying to remove xpath " + xpath,
ex);
}
}
public void removeAll(String xpath) {
removeAll(xpath, compileExpression(xpath));
}
public void removeAll(String xpath, Expression expr) {
try {
ArrayList list = new ArrayList();
Iterator it = expr.iteratePointers(getEvalContext());
while (it.hasNext()) {
list.add(it.next());
}
Collections.sort(list, ReverseComparator.INSTANCE);
it = list.iterator();
if (it.hasNext()) {
NodePointer pointer = (NodePointer) it.next();
pointer.remove();
while (it.hasNext()) {
removePath(((NodePointer) it.next()).asPath());
}
}
}
catch (Throwable ex) {
throw new JXPathException(
"Exception trying to remove all for xpath " + xpath,
ex);
}
}
public JXPathContext getRelativeContext(Pointer pointer) {
Object contextBean = pointer.getNode();
if (contextBean == null) {
throw new JXPathException(
"Cannot create a relative context for a non-existent node: "
+ pointer);
}
return new JXPathContextReferenceImpl(this, contextBean, pointer);
}
public Pointer getContextPointer() {
return contextPointer;
}
private NodePointer getAbsoluteRootPointer() {
return (NodePointer) rootPointer;
}
private EvalContext getEvalContext() {
return new InitialContext(new RootContext(this,
(NodePointer) getContextPointer()));
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
public EvalContext getAbsoluteRootContext() {
return new InitialContext(new RootContext(this,
getAbsoluteRootPointer()));
}
public NodePointer getVariablePointer(QName name) {
return NodePointer.newNodePointer(name, VariablePointerFactory
.contextWrapper(this), getLocale());
}
public Function getFunction(QName functionName, Object[] parameters) {
String namespace = functionName.getPrefix();
String name = functionName.getName();
JXPathContext funcCtx = this;
Function func = null;
Functions funcs;
while (funcCtx != null) {
funcs = funcCtx.getFunctions();
if (funcs != null) {
func = funcs.getFunction(namespace, name, parameters);
if (func != null) {
return func;
}
}
funcCtx = funcCtx.getParentContext();
}
throw new JXPathFunctionNotFoundException(
"Undefined function: " + functionName.toString());
}
public void registerNamespace(String prefix, String namespaceURI) {
if (namespaceResolver.isSealed()) {
namespaceResolver = (NamespaceResolver) namespaceResolver.clone();
}
namespaceResolver.registerNamespace(prefix, namespaceURI);
}
public String getNamespaceURI(String prefix) {
return namespaceResolver.getNamespaceURI(prefix);
}
/**
* {@inheritDoc}
* @see org.apache.commons.jxpath.JXPathContext#getPrefix(java.lang.String)
*/
public String getPrefix(String namespaceURI) {
return namespaceResolver.getPrefix(namespaceURI);
}
public void setNamespaceContextPointer(Pointer pointer) {
if (namespaceResolver.isSealed()) {
namespaceResolver = (NamespaceResolver) namespaceResolver.clone();
}
namespaceResolver.setNamespaceContextPointer((NodePointer) pointer);
}
public Pointer getNamespaceContextPointer() {
return namespaceResolver.getNamespaceContextPointer();
}
public NamespaceResolver getNamespaceResolver() {
namespaceResolver.seal();
return namespaceResolver;
}
/**
* Checks if existenceCheckClass exists on the class path. If so, allocates
* an instance of the specified class, otherwise returns null.
*/
public static Object allocateConditionally(
String className,
String existenceCheckClassName)
{
try {
try {
Class
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import org.apache.commons.jxpath.BasicNodeSet;
import org.apache.commons.jxpath.ExpressionContext;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.NodeSet;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.axes.RootContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.apache.commons.jxpath.util.ReverseComparator;
/**
* An XPath evaluation context.
*
* When evaluating a path, a chain of EvalContexts is created, each context in
* the chain representing a step of the path. Subclasses of EvalContext
* implement behavior of various XPath axes: "child::", "parent::" etc.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public abstract class EvalContext implements ExpressionContext, Iterator {
protected EvalContext parentContext;
protected RootContext rootContext;
protected int position = 0;
private boolean startedSet
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>Iteration = false;
private boolean done = false;
private boolean hasPerformedIteratorStep = false;
private Iterator pointerIterator;
public EvalContext(EvalContext parentContext) {
this.parentContext = parentContext;
}
public Pointer getContextNodePointer() {
return getCurrentNodePointer();
}
public JXPathContext getJXPathContext() {
return getRootContext().getJXPathContext();
}
public int getPosition() {
return position;
}
/**
* Determines the document order for this context.
*
* @return 1 ascending order, -1 descending order,
* 0 - does not require ordering
*/
public int getDocumentOrder() {
return parentContext != null && parentContext.isChildOrderingRequired() ? 1 : 0;
}
/**
* Even if this context has the natural ordering and therefore does
* not require collecting and sorting all nodes prior to returning them,
* such operation may be required for any child context.
*/
public boolean isChildOrderingRequired() {
// Default behavior: if this context needs to be ordered,
// the children need to be ordered too
return getDocumentOrder() != 0;
}
/**
* Returns true if there are mode nodes matching the context's constraints.
*/
public boolean hasNext() {
if (pointerIterator != null) {
return pointerIterator.hasNext();
}
if (getDocumentOrder() != 0) {
return constructIterator();
}
if (!done && !hasPerformedIteratorStep) {
performIteratorStep();
}
return !done;
}
/**
* Returns the next node pointer in the context
*/
public Object next() {
if (pointerIterator != null) {
return pointerIterator.next();
}
if (getDocumentOrder() != 0) {
if (!constructIterator()) {
throw new NoSuchElementException();
}
return pointerIterator.next();
}
if (!done && !hasPerformedIteratorStep) {
performIteratorStep();
}
if (done) {
throw new NoSuchElementException();
}
hasPerformedIteratorStep = false;
return getCurrentNodePointer();
}
/**
* Moves the iterator forward by one position
*/
private void performIteratorStep() {
done = true;
if (position != 0 && nextNode()) {
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> done = false;
}
else {
while (nextSet()) {
if (nextNode()) {
done = false;
break;
}
}
}
hasPerformedIteratorStep = true;
}
/**
* Operation is not supported
*/
public void remove() {
throw new UnsupportedOperationException(
"JXPath iterators cannot remove nodes");
}
private boolean constructIterator() {
HashSet set = new HashSet();
ArrayList list = new ArrayList();
while (nextSet()) {
while (nextNode()) {
NodePointer pointer = getCurrentNodePointer();
if (!set.contains(pointer)) {
set.add(pointer);
list.add(pointer);
}
}
}
if (list.isEmpty()) {
return false;
}
sortPointers(list);
pointerIterator = list.iterator();
return true;
}
/**
* Sort a list of pointers based on document order.
* @param l the list to sort.
*/
protected void sortPointers(List l) {
switch (getDocumentOrder()) {
case 1:
Collections.sort(l);
break;
case -1:
Collections.sort(l, ReverseComparator.INSTANCE);
break;
}
}
/**
* Returns the list of all Pointers in this context for the current
* position of the parent context.
*/
public List getContextNodeList() {
int pos = position;
if (pos != 0) {
reset();
}
List list = new ArrayList();
while (nextNode()) {
list.add(getCurrentNodePointer());
}
if (pos != 0) {
setPosition(pos);
}
else {
reset();
}
return list;
}
/**
* Returns the list of all Pointers in this context for all positions
* of the parent contexts. If there was an ongoing iteration over
* this context, the method should not be called.
*/
public NodeSet getNodeSet() {
if (position != 0) {
throw new JXPathException(
"Simultaneous operations: "
+ "should not request pointer list while "
+ "iterating over an EvalContext");
}
BasicNodeSet set = new BasicNodeSet();
while (nextSet()) {
while (nextNode()) {
set.add((Pointer
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>)getCurrentNodePointer().clone());
}
}
return set;
}
/**
* Typically returns the NodeSet by calling getNodeSet(),
* but will be overridden for contexts that more naturally produce
* individual values, e.g. VariableContext
*/
public Object getValue() {
return getNodeSet();
}
public String toString() {
Pointer ptr = getContextNodePointer();
return ptr == null ? "Empty expression context" : "Expression context [" + getPosition()
+ "] " + ptr.asPath();
}
/**
* Returns the root context of the path, which provides easy
* access to variables and functions.
*/
public RootContext getRootContext() {
if (rootContext == null) {
rootContext = parentContext.getRootContext();
}
return rootContext;
}
/**
* Sets current position = 0, which is the pre-iteration state.
*/
public void reset() {
position = 0;
}
public int getCurrentPosition() {
return position;
}
/**
* Returns the first encountered Pointer that matches the current
* context's criteria.
*/
public Pointer getSingleNodePointer() {
reset();
while (nextSet()) {
if (nextNode()) {
return getCurrentNodePointer();
}
}
return null;
}
/**
* Returns the current context node. Undefined before the beginning
* of the iteration.
*/
public abstract NodePointer getCurrentNodePointer();
/**
* Returns true if there is another sets of objects to interate over.
* Resets the current position and node.
*/
public boolean nextSet() {
reset(); // Restart iteration within the set
// Most of the time you have one set per parent node
// First time this method is called, we should look for
// the first parent set that contains at least one node.
if (!startedSetIteration) {
startedSetIteration = true;
while (parentContext.nextSet()) {
if (parentContext.nextNode()) {
return true;
}
}
return false;
}
// In subsequent calls, we see if the parent context
// has any nodes left in the current set
if (parentContext.nextNode()) {
return true;
}
// If not, we look for the next set that contains
// at
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath;
/**
* A delegate of JXPathContext that implements the XPath <code>"id()"</code>
* function. This delegate is only used when there is no default implementation
* of the <code>id()</code> function. For example, it is <i>not</i> used
* when the root of the context is a DOM Node.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public interface IdentityManager {
/**
* Finds a node by its ID.
*/
Pointer getPointerByID(JXPathContext context, String id);
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>) node).getParent();
}
if (node instanceof Comment) {
return (Element) ((Comment) node).getParent();
}
return null;
}
public NodePointer createChild(
JXPathContext context,
QName name,
int index)
{
if (index == WHOLE_COLLECTION) {
index = 0;
}
boolean success =
getAbstractFactory(context).createObject(
context,
this,
node,
name.toString(),
index);
if (success) {
NodeTest nodeTest;
String prefix = name.getPrefix();
String namespaceURI = prefix == null ? null : context
.getNamespaceURI(prefix);
nodeTest = new NodeNameTest(name, namespaceURI);
NodeIterator it =
childIterator(nodeTest, false, null);
if (it != null && it.setPosition(index + 1)) {
return it.getNodePointer();
}
}
throw new JXPathAbstractFactoryException("Factory could not create "
+ "a child node for path: " + asPath() + "/" + name + "["
+ (index + 1) + "]");
}
public NodePointer createChild(
JXPathContext context, QName name, int index, Object value)
{
NodePointer ptr = createChild(context, name, index);
ptr.setValue(value);
return ptr;
}
public NodePointer createAttribute(JXPathContext context, QName name) {
if (!(node instanceof Element)) {
return super.createAttribute(context, name);
}
Element element = (Element) node;
String prefix = name.getPrefix();
if (prefix != null) {
String namespaceUri = getNamespaceResolver().getNamespaceURI(prefix);
if (namespaceUri == null) {
throw new JXPathException(
"Unknown namespace prefix: " + prefix);
}
Namespace ns = Namespace.getNamespace(prefix, namespaceUri);
Attribute attr = element.getAttribute(name.getName(), ns);
if (attr == null) {
element.setAttribute(name.getName(), "", ns);
}
}
else {
Attribute attr = element.getAttribute(name.getName());
if (attr == null) {
element.setAttribute(name.getName(), "");
}
}
NodeIterator it = attributeIterator(name
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>);
it.setPosition(1);
return it.getNodePointer();
}
public void remove() {
Element parent = nodeParent(node);
if (parent == null) {
throw new JXPathException("Cannot remove root JDOM node");
}
parent.getContent().remove(node);
}
public String asPath() {
if (id != null) {
return "id('" + escape(id) + "')";
}
StringBuffer buffer = new StringBuffer();
if (parent != null) {
buffer.append(parent.asPath());
}
if (node instanceof Element) {
// If the parent pointer is not a JDOMNodePointer, it is
// the parent's responsibility to produce the node test part
// of the path
if (parent instanceof JDOMNodePointer) {
if (buffer.length() == 0
|| buffer.charAt(buffer.length() - 1) != '/') {
buffer.append('/');
}
String nsURI = getNamespaceURI();
String ln = JDOMNodePointer.getLocalName(node);
if (nsURI == null) {
buffer.append(ln);
buffer.append('[');
buffer.append(getRelativePositionByName()).append(']');
}
else {
String prefix = getNamespaceResolver().getPrefix(nsURI);
if (prefix != null) {
buffer.append(prefix);
buffer.append(':');
buffer.append(ln);
buffer.append('[');
buffer.append(getRelativePositionByName());
buffer.append(']');
}
else {
buffer.append("node()");
buffer.append('[');
buffer.append(getRelativePositionOfElement());
buffer.append(']');
}
}
}
}
else if (node instanceof Text || node instanceof CDATA) {
buffer.append("/text()");
buffer.append('[').append(getRelativePositionOfTextNode()).append(
']');
}
else if (node instanceof ProcessingInstruction) {
String target = ((ProcessingInstruction) node).getTarget();
buffer.append("/processing-instruction(\'").append(target).append(
"')");
buffer.append('[').append(getRelativePositionOfPI(target)).append(
']');
}
return buffer.toString();
}
private String escape(String string
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that returns the current node from the parent context if the
* test succeeds.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class SelfContext extends EvalContext {
private NodeTest nodeTest;
private boolean startedSet = false;
private NodePointer nodePointer;
public SelfContext(EvalContext parentContext, NodeTest nodeTest) {
super(parentContext);
this.nodeTest = nodeTest;
}
public Pointer getSingleNodePointer() {
return parentContext.getSingleNodePointer();
}
public NodePointer getCurrentNodePointer() {
if (position == 0) {
if (!setPosition(1)) {
return null;
}
}
return nodePointer;
}
public boolean nextNode() {
return setPosition(getCurrentPosition() + 1);
}
public void reset() {
super.reset();
startedSet = false;
}
public boolean setPosition(int position) {
if (position != 1) {
return false;
}
super.setPosition(position);
if (!
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import junit.framework.TestCase;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* Abstract superclass for various JXPath tests.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public abstract class JXPathTestCase extends TestCase {
/**
* Construct a new instance of this test case.
*
* @param name Name of the test case
*/
public JXPathTestCase(String name) {
super(name);
Locale.setDefault(Locale.US);
}
protected void assertXPathValue(JXPathContext ctx,
String xpath, Object expected)
{
ctx.setLenient(false);
Object actual = ctx.getValue(xpath);
assertEquals("Evaluating <" + xpath + ">", expected, actual);
}
protected void assertXPathValue(JXPathContext ctx,
String xpath, Object expected, Class resultType)
{
ctx.setLenient(false);
Object actual = ctx.getValue(xpath, resultType);
assertEquals("Evaluating <" + xpath + ">", expected, actual);
}
protected void assertXPath
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>ValueLenient(JXPathContext ctx,
String xpath, Object expected)
{
ctx.setLenient(true);
Object actual = ctx.getValue(xpath);
ctx.setLenient(false);
assertEquals("Evaluating lenient <" + xpath + ">", expected, actual);
}
protected void assertXPathSetValue(JXPathContext ctx,
String xpath, Object value)
{
assertXPathSetValue(ctx, xpath, value, value);
}
protected void assertXPathSetValue(JXPathContext ctx,
String xpath, Object value, Object expected)
{
ctx.setValue(xpath, value);
Object actual = ctx.getValue(xpath);
assertEquals("Modifying <" + xpath + ">", expected, actual);
}
protected void assertXPathCreatePath(JXPathContext ctx,
String xpath,
Object expectedValue, String expectedPath)
{
Pointer pointer = ctx.createPath(xpath);
assertEquals("Creating path <" + xpath + ">",
expectedPath, pointer.asPath());
assertEquals("Creating path (pointer value) <" + xpath + ">",
expectedValue, pointer.getValue());
assertEquals("Creating path (context value) <" + xpath + ">",
expectedValue, ctx.getValue(pointer.asPath()));
}
protected void assertXPathCreatePathAndSetValue(JXPathContext ctx,
String xpath, Object value,
String expectedPath)
{
Pointer pointer = ctx.createPathAndSetValue(xpath, value);
assertEquals("Creating path <" + xpath + ">",
expectedPath, pointer.asPath());
assertEquals("Creating path (pointer value) <" + xpath + ">",
value, pointer.getValue());
assertEquals("Creating path (context value) <" + xpath + ">",
value, ctx.getValue(pointer.asPath()));
}
protected void assertXPathPointer(JXPathContext ctx,
String xpath, String expected)
{
ctx.setLenient(false);
Pointer pointer = ctx.getPointer(xpath);
String actual = pointer.toString();
assertEquals("Evaluating pointer <" + xpath + ">", expected, actual);
}
protected void assertXPathPointerLenient(JXPathContext ctx,
String xpath, String expected)
{
ctx.setLenient(true);
Pointer pointer = ctx.getPointer(xpath);
String
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> actual = pointer.toString();
assertEquals("Evaluating pointer <" + xpath + ">", expected, actual);
}
protected void assertXPathValueAndPointer(JXPathContext ctx,
String xpath, Object expectedValue, String expectedPointer)
{
assertXPathValue(ctx, xpath, expectedValue);
assertXPathPointer(ctx, xpath, expectedPointer);
}
protected void assertXPathValueIterator(JXPathContext ctx,
String xpath, Collection expected)
{
Collection actual;
if (expected instanceof List) {
actual = new ArrayList();
}
else {
actual = new HashSet();
}
Iterator it = ctx.iterate(xpath);
while (it.hasNext()) {
actual.add(it.next());
}
assertEquals("Evaluating value iterator <" + xpath + ">",
expected, actual);
}
protected void assertXPathPointerIterator(
JXPathContext ctx,
String xpath,
Collection expected)
{
Collection actual;
if (expected instanceof List) {
actual = new ArrayList();
}
else {
actual = new HashSet();
}
Iterator it = ctx.iteratePointers(xpath);
while (it.hasNext()) {
Pointer pointer = (Pointer) it.next();
actual.add(pointer.toString());
}
assertEquals(
"Evaluating pointer iterator <" + xpath + ">",
expected,
actual);
}
protected void assertDocumentOrder(
JXPathContext context,
String path1,
String path2,
int expected)
{
NodePointer np1 = (NodePointer) context.getPointer(path1);
NodePointer np2 = (NodePointer) context.getPointer(path2);
int res = np1.compareTo(np2);
if (res < 0) {
res = -1;
}
else if (res > 0) {
res = 1;
}
assertEquals(
"Comparing paths '" + path1 + "' and '" + path2 + "'",
expected,
res);
}
protected void assertXPathValueType(
JXPathContext ctx,
String xpath,
Class clazz)
{
ctx.setLenient(false);
Object actual = ctx.getValue(xpath);
assertTrue("Evaluating <" + xpath + "> = " + actual.getClass(),
clazz.isAssignableFrom(actual
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>.getClass()));
}
protected void assertXPathNodeType(
JXPathContext ctx,
String xpath,
Class clazz)
{
ctx.setLenient(false);
Pointer actual = ctx.getPointer(xpath);
assertTrue("Evaluating <" + xpath + "> = " + actual.getNode().getClass(),
clazz.isAssignableFrom(actual.getNode().getClass()));
}
protected static List list() {
return Collections.EMPTY_LIST;
}
protected static List list(Object o1) {
List list = new ArrayList();
list.add(o1);
return list;
}
protected static List list(Object o1, Object o2) {
List list = new ArrayList();
list.add(o1);
list.add(o2);
return list;
}
protected static List list(Object o1, Object o2, Object o3) {
List list = new ArrayList();
list.add(o1);
list.add(o2);
list.add(o3);
return list;
}
protected static Set set(Object o1, Object o2, Object o3) {
Set list = new HashSet();
list.add(o1);
list.add(o2);
list.add(o3);
return list;
}
protected static List list(Object o1, Object o2, Object o3, Object o4) {
List list = new ArrayList();
list.add(o1);
list.add(o2);
list.add(o3);
list.add(o4);
return list;
}
protected static Set set(Object o1, Object o2, Object o3, Object o4) {
Set list = new HashSet();
list.add(o1);
list.add(o2);
list.add(o3);
list.add(o4);
return list;
}
protected static List list(Object o1, Object o2, Object o3,
Object o4, Object o5)
{
List list = new ArrayList();
list.add(o1);
list.add(o2);
list.add(o3);
list.add(o4);
list.add(o5);
return list;
}
protected static Set set
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* Namespace resolver for JXPathContextReferenceImpl.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class NamespaceResolver implements Cloneable {
final protected NamespaceResolver parent;
protected HashMap namespaceMap = new HashMap();
protected HashMap reverseMap;
protected NodePointer pointer;
private boolean sealed;
/**
* Find the namespace prefix for the specified namespace URI and NodePointer.
* @param pointer
* @param namespaceURI
* @return prefix if found
* @since JXPath 1.3
*/
protected static String getPrefix(NodePointer pointer, String namespaceURI) {
NodePointer currentPointer = pointer;
while (currentPointer != null) {
NodeIterator ni = currentPointer.namespaceIterator();
for (int position = 1; ni != null && ni.setPosition(position); position++) {
NodePointer nsPointer = ni.getNodePointer();
String uri = nsPointer.getNamespaceURI();
if (uri.equals(namespaceURI)) {
String prefix = nsPointer.getName().getName();
if (!"".equals
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>(prefix)) {
return prefix;
}
}
}
currentPointer = pointer.getParent();
}
return null;
}
/**
* Create a new NamespaceResolver.
*/
public NamespaceResolver() {
this(null);
}
/**
* Create a new NamespaceResolver.
* @param parent
*/
public NamespaceResolver(NamespaceResolver parent) {
this.parent = parent;
}
/**
* Registers a namespace prefix.
*
* @param prefix A namespace prefix
* @param namespaceURI A URI for that prefix
*/
public synchronized void registerNamespace(String prefix, String namespaceURI) {
if (isSealed()) {
throw new IllegalStateException(
"Cannot register namespaces on a sealed NamespaceResolver");
}
namespaceMap.put(prefix, namespaceURI);
reverseMap = null;
}
/**
* Register a namespace for the expression context.
* @param pointer the Pointer to set.
*/
public void setNamespaceContextPointer(NodePointer pointer) {
this.pointer = pointer;
}
/**
* Get the namespace context pointer.
* @return Pointer
*/
public Pointer getNamespaceContextPointer() {
if (pointer == null && parent != null) {
return parent.getNamespaceContextPointer();
}
return pointer;
}
/**
* Given a prefix, returns a registered namespace URI. If the requested
* prefix was not defined explicitly using the registerNamespace method,
* JXPathContext will then check the context node to see if the prefix is
* defined there. See
* {@link #setNamespaceContextPointer(NodePointer) setNamespaceContextPointer}.
*
* @param prefix The namespace prefix to look up
* @return namespace URI or null if the prefix is undefined.
*/
public synchronized String getNamespaceURI(String prefix) {
String uri = getExternallyRegisteredNamespaceURI(prefix);
return uri == null && pointer != null ? pointer.getNamespaceURI(prefix)
: uri;
}
/**
* Given a prefix, returns an externally registered namespace URI.
*
* @param prefix The namespace prefix to look up
* @return namespace URI or null if the prefix is undefined.
* @since JXPath 1.3
*/
protected synchronized String getExternallyRegisteredNamespaceURI(
String prefix) {
String uri
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> = (String) namespaceMap.get(prefix);
return uri == null && parent != null ? parent
.getExternallyRegisteredNamespaceURI(prefix) : uri;
}
/**
* Get the prefix associated with the specifed namespace URI.
* @param namespaceURI the ns URI to check.
* @return String prefix
*/
public synchronized String getPrefix(String namespaceURI) {
String prefix = getExternallyRegisteredPrefix(namespaceURI);
return prefix == null && pointer != null ? getPrefix(pointer,
namespaceURI) : prefix;
}
/**
* Get the nearest prefix found that matches an externally-registered namespace.
* @param namespaceURI
* @return String prefix if found.
* @since JXPath 1.3
*/
protected synchronized String getExternallyRegisteredPrefix(String namespaceURI) {
if (reverseMap == null) {
reverseMap = new HashMap();
Iterator it = namespaceMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
reverseMap.put(entry.getValue(), entry.getKey());
}
}
String prefix = (String) reverseMap.get(namespaceURI);
return prefix == null && parent != null ? parent
.getExternallyRegisteredPrefix(namespaceURI) : prefix;
}
/**
* Learn whether this NamespaceResolver has been sealed.
* @return
*/
public boolean isSealed() {
return sealed;
}
/**
* Seal this {@link NamespaceResolver}.
*/
public void seal() {
sealed = true;
if (parent != null) {
parent.seal();
}
}
/**
* {@inheritDoc}
* @see java.lang.Object#clone()
*/
public Object clone() {
try {
NamespaceResolver result = (NamespaceResolver) super.clone();
result.sealed = false;
return result;
}
catch (CloneNotSupportedException e) {
// Of course, it's supported.
e.printStackTrace();
return null;
}
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> may be qualified.
* @param index is used if the pointer represents a collection element. You
* may need to expand or even create the collection to accommodate the new
* element.
*
* @return true if the object was successfully created
*/
public boolean createObject(JXPathContext context, Pointer pointer,
Object parent, String name, int index)
{
return false;
}
/**
* Declare the specified variable
*
* @param context hosts variable pools. See
* {@link JXPathContext#getVariables() JXPathContext.getVariables()}
* @param name is the name of the variable without the "$" sign
*
* @return true if the variable was successfully defined
*/
public boolean declareVariable(JXPathContext context, String name) {
return false;
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.model.dom;
import org.apache.commons.jxpath.AbstractFactory;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
/**
* Test AbstractFactory.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class TestDOMFactory extends AbstractFactory {
/**
* Return <b>false</b> if this factory cannot create the requested object.
*/
public boolean createObject(
JXPathContext context,
Pointer pointer,
Object parent,
String name,
int index)
{
if (name.equals("location")
|| name.equals("address")
|| name.equals("street")) {
addDOMElement((Node) parent, index, name, null);
return true;
}
if (name.startsWith("price:")) {
String namespaceURI = context.getNamespaceURI("price");
addDOMElement((Node) parent, index, name, namespaceURI);
return true;
}
return false;
}
private void addDOMElement(Node parent, int index, String tag, String namespaceURI) {
Node child = parent.getFirstChild();
int count = 0;
while (child != null) {
if (child.getNode
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.model.NodeIterator;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that walks the "attribute::" axis.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class AttributeContext extends EvalContext {
private NodeTest nodeTest;
private boolean setStarted = false;
private NodeIterator iterator;
private NodePointer currentNodePointer;
/**
* @param parentContext represents the previous step on the path
* @param nodeTest is the name of the attribute we are looking for
*/
public AttributeContext(EvalContext parentContext, NodeTest nodeTest) {
super(parentContext);
this.nodeTest = nodeTest;
}
public NodePointer getCurrentNodePointer() {
return currentNodePointer;
}
public void reset() {
setStarted = false;
iterator = null;
super.reset();
}
public boolean setPosition(int position) {
if (position < getCurrentPosition()) {
reset();
}
while (
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>getCurrentPosition() < position) {
if (!nextNode()) {
return false;
}
}
return true;
}
public boolean nextNode() {
super.setPosition(getCurrentPosition() + 1);
if (!setStarted) {
setStarted = true;
if (!(nodeTest instanceof NodeNameTest)) {
return false;
}
QName name = ((NodeNameTest) nodeTest).getNodeName();
iterator =
parentContext.getCurrentNodePointer().attributeIterator(name);
}
if (iterator == null) {
return false;
}
if (!iterator.setPosition(iterator.getPosition() + 1)) {
return false;
}
currentNodePointer = iterator.getNodePointer();
return true;
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.compiler;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.Compiler;
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.axes.AncestorContext;
import org.apache.commons.jxpath.ri.axes.AttributeContext;
import org.apache.commons.jxpath.ri.axes.ChildContext;
import org.apache.commons.jxpath.ri.axes.DescendantContext;
import org.apache.commons.jxpath.ri.axes.InitialContext;
import org.apache.commons.jxpath.ri.axes.NamespaceContext;
import org.apache.commons.jxpath.ri.axes.ParentContext;
import org.apache.commons.jxpath.ri.axes.PrecedingOrFollowingContext;
import org.apache.commons.jxpath.ri.axes.PredicateContext;
import org.apache.commons.jxpath.ri.axes.SelfContext;
import org.apache.commons.jxpath.ri.axes.SimplePathInterpreter;
import org.apache.commons.jxpath.ri.axes.UnionContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* @author Dmitri Plotnikov
*
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>step.getPredicates());
}
return false;
}
protected boolean areBasicPredicates(Expression predicates[]) {
if (predicates != null && predicates.length != 0) {
boolean firstIndex = true;
for (int i = 0; i < predicates.length; i++) {
if (predicates[i] instanceof NameAttributeTest) {
if (((NameAttributeTest) predicates[i])
.getNameTestExpression()
.isContextDependent()) {
return false;
}
}
else if (predicates[i].isContextDependent()) {
return false;
}
else {
if (!firstIndex) {
return false;
}
firstIndex = false;
}
}
}
return true;
}
/**
* Given a root context, walks a path therefrom and finds the
* pointer to the first element matching the path.
*/
protected Pointer getSingleNodePointerForSteps(EvalContext context) {
if (steps.length == 0) {
return context.getSingleNodePointer();
}
if (isSimplePath()) {
NodePointer ptr = (NodePointer) context.getSingleNodePointer();
return SimplePathInterpreter.interpretSimpleLocationPath(
context,
ptr,
steps);
}
return searchForPath(context);
}
/**
* The idea here is to return a NullPointer rather than null if that's at
* all possible. Take for example this path: "//map/key". Let's say, "map"
* is an existing node, but "key" is not there. We will create a
* NullPointer that can be used to set/create the "key" property.
* <p>
* However, a path like "//key" would still produce null, because we have
* no way of knowing where "key" would be if it existed.
* </p>
* <p>
* To accomplish this, we first try the path itself. If it does not find
* anything, we chop off last step of the path, as long as it is a simple
* one like child:: or attribute:: and try to evaluate the truncated path.
* If it finds exactly one node - create a NullPointer and return. If it
* fails, chop off another step and repeat. If it finds more than one
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>
* location - return null.
* </p>
*/
private Pointer searchForPath(EvalContext context) {
EvalContext ctx = buildContextChain(context, steps.length, true);
Pointer pointer = ctx.getSingleNodePointer();
if (pointer != null) {
return pointer;
}
for (int i = steps.length; --i > 0;) {
if (!isSimpleStep(steps[i])) {
return null;
}
ctx = buildContextChain(context, i, true);
if (ctx.hasNext()) {
Pointer partial = (Pointer) ctx.next();
if (ctx.hasNext()) {
// If we find another location - the search is
// ambiguous, so we report failure
return null;
}
if (partial instanceof NodePointer) {
return SimplePathInterpreter.createNullPointer(
context,
(NodePointer) partial,
steps,
i);
}
}
}
return null;
}
/**
* Given a root context, walks a path therefrom and builds a context
* that contains all nodes matching the path.
*/
protected EvalContext evalSteps(EvalContext context) {
return buildContextChain(context, steps.length, false);
}
private EvalContext buildContextChain(
EvalContext context,
int stepCount,
boolean createInitialContext)
{
if (createInitialContext) {
context = new InitialContext(context);
}
if (steps.length == 0) {
return context;
}
for (int i = 0; i < stepCount; i++) {
context =
createContextForStep(
context,
steps[i].getAxis(),
steps[i].getNodeTest());
Expression predicates[] = steps[i].getPredicates();
if (predicates != null) {
for (int j = 0; j < predicates.length; j++) {
if (j != 0) {
context = new UnionContext(context, new EvalContext[]{context});
}
context = new PredicateContext(context, predicates[j]);
}
}
}
return context;
}
/**
* Different axes are serviced by different contexts. This method
* allocates the right context for the supplied step.
*/
protected EvalContext createContextForStep(
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* A simple implementation of NodeSet that behaves as a collection of pointers.
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class BasicNodeSet implements NodeSet {
private List pointers = new ArrayList();
private List readOnlyPointers;
private List nodes;
private List values;
public void add(Pointer pointer) {
pointers.add(pointer);
readOnlyPointers = null;
}
public void add(NodeSet nodeSet) {
pointers.addAll(nodeSet.getPointers());
readOnlyPointers = null;
}
public void remove(Pointer pointer) {
pointers.remove(pointer);
readOnlyPointers = null;
}
public List getPointers() {
if (readOnlyPointers == null) {
readOnlyPointers = Collections.unmodifiableList(pointers);
}
return readOnlyPointers;
}
public List getNodes() {
if (nodes == null) {
nodes = new ArrayList();
for (int i = 0; i < pointers.size(); i++) {
Pointer pointer = (Pointer) pointers.get(i);
nodes.add(pointer.getValue());
}
nodes = Collections.unmodifiableList(nodes);
}
return nodes;
}
public List getValues() {
if (values == null) {
values = new ArrayList();
for (
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>int i = 0; i < pointers.size(); i++) {
Pointer pointer = (Pointer) pointers.get(i);
values.add(pointer.getValue());
}
values = Collections.unmodifiableList(values);
}
return values;
}
public String toString() {
return pointers.toString();
}
}
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath;
import java.util.List;
/**
* If an extenstion function has an argument of type ExpressionContext,
* it can gain access to the current node of an XPath expression context.
* <p>
* Example:
* <blockquote><pre>
* public class MyExtenstionFunctions {
* public static String objectType(ExpressionContext context){
* Object value = context.getContextNodePointer().getValue();
* if (value == null){
* return "null";
* }
* return value.getClass().getName();
* }
* }
* </pre></blockquote>
*
* You can then register this extension function using a {@link ClassFunctions
* ClassFunctions} object and call it like this:
* <blockquote><pre>
* "/descendent-or-self::node()[ns:objectType() = 'java.util.Date']"
* </pre></blockquote>
* This expression will find all nodes of the graph that are dates.
*/
public interface ExpressionContext {
/**
* Get the JXPathContext in which this function is being evaluated.
*
* @return A list representing the current context nodes.
*/
JXPathContext getJXPathContext();
/**
* Get the current context node.
*
* @return The current context node pointer.
*/
Pointer getContextNodePointer();
/**
* Get the current context node list. Each
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.model;
import java.util.HashSet;
import java.util.Locale;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.NodeSet;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.Compiler;
import org.apache.commons.jxpath.ri.JXPathContextReferenceImpl;
import org.apache.commons.jxpath.ri.NamespaceResolver;
import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.compiler.NodeNameTest;
import org.apache.commons.jxpath.ri.compiler.NodeTest;
import org.apache.commons.jxpath.ri.compiler.NodeTypeTest;
import org.apache.commons.jxpath.ri.model.beans.NullPointer;
/**
* Common superclass for Pointers of all kinds. A NodePointer maps to
* a deterministic XPath that represents the location of a node in an
* object graph. This XPath uses only simple axes: child, namespace and
* attribute and only simple, context-independent predicates.
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public abstract class NodePointer implements Pointer {
public static final int WHO
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>// return null;
// }
//
/**
* Returns a NodeIterator that iterates over all children or all children
* that match the given NodeTest, starting with the specified one.
*/
public NodeIterator childIterator(
NodeTest test,
boolean reverse,
NodePointer startWith)
{
NodePointer valuePointer = getValuePointer();
return valuePointer == null || valuePointer == this ? null
: valuePointer.childIterator(test, reverse, startWith);
}
/**
* Returns a NodeIterator that iterates over all attributes of the current
* node matching the supplied node name (could have a wildcard).
* May return null if the object does not support the attributes.
*/
public NodeIterator attributeIterator(QName qname) {
NodePointer valuePointer = getValuePointer();
return valuePointer == null || valuePointer == this ? null
: valuePointer.attributeIterator(qname);
}
/**
* Returns a NodeIterator that iterates over all namespaces of the value
* currently pointed at.
* May return null if the object does not support the namespaces.
*/
public NodeIterator namespaceIterator() {
return null;
}
/**
* Returns a NodePointer for the specified namespace. Will return null
* if namespaces are not supported.
* Will return UNKNOWN_NAMESPACE if there is no such namespace.
*/
public NodePointer namespacePointer(String namespace) {
return null;
}
/**
* Decodes a namespace prefix to the corresponding URI.
*/
public String getNamespaceURI(String prefix) {
return null;
}
/**
* Returns the namespace URI associated with this Pointer.
*/
public String getNamespaceURI() {
return null;
}
/**
* Returns true if the supplied prefix represents the
* default namespace in the context of the current node.
*/
protected boolean isDefaultNamespace(String prefix) {
if (prefix == null) {
return true;
}
String namespace = getNamespaceURI(prefix);
return namespace != null && namespace.equals(getDefaultNamespaceURI());
}
protected String getDefaultNamespaceURI() {
return null;
}
/**
* Locates a node by ID.
*/
public Pointer getPointerByID(JXPathContext context, String id) {
return context.getPointerByID(id);
}
/**
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS> * Locates a node by key and value.
*/
public Pointer getPointerByKey(
JXPathContext context,
String key,
String value)
{
return context.getPointerByKey(key, value);
}
/**
* Find a NodeSet by key/value.
* @param context
* @param key
* @param value
* @return NodeSet
*/
public NodeSet getNodeSetByKey(JXPathContext context, String key, Object value) {
return context.getNodeSetByKey(key, value);
}
/**
* Returns an XPath that maps to this Pointer.
*/
public String asPath() {
// If the parent of this node is a container, it is responsible
// for appended this node's part of the path.
if (parent != null && parent.isContainer()) {
return parent.asPath();
}
StringBuffer buffer = new StringBuffer();
if (parent != null) {
buffer.append(parent.asPath());
}
if (buffer.length() == 0
|| buffer.charAt(buffer.length() - 1) != '/') {
buffer.append('/');
}
if (attribute) {
buffer.append('@');
}
buffer.append(getName());
if (index != WHOLE_COLLECTION && isCollection()) {
buffer.append('[').append(index + 1).append(']');
}
return buffer.toString();
}
public Object clone() {
try {
NodePointer ptr = (NodePointer) super.clone();
if (parent != null) {
ptr.parent = (NodePointer) parent.clone();
}
return ptr;
}
catch (CloneNotSupportedException ex) {
// Of course it is supported
ex.printStackTrace();
}
return null;
}
public String toString() {
return asPath();
}
public int compareTo(Object object) {
if (object == this) {
return 0;
}
// Let it throw a ClassCastException
NodePointer pointer = (NodePointer) object;
if (parent == pointer.parent) {
return parent == null ? 0 : parent.compareChildNodePointers(this, pointer);
}
// Task 1: find the common parent
int depth1 = 0;
NodePointer p1 = this;
JxPath, 15
<FILEB>
<CHANGES>
import java.util.Iterator;
<CHANGEE>
<CHANGES>
import org.apache.commons.jxpath.Pointer;
<CHANGEE>
<CHANGES>
<CHANGEE>
<CHANGES>
sortPointers(pointers);
for (Iterator it = pointers.iterator(); it.hasNext();) {
nodeSet.add((Pointer) it.next());
}
<CHANGEE>
<CHANGES>
}<CHANGEE>
<FILEE>
<FILEB>
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.axes;
import java.util.ArrayList;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.BasicNodeSet;
<CHANGES>
<CHANGEE>
import org.apache.commons.jxpath.ri.EvalContext;
import org.apache.commons.jxpath.ri.model.NodePointer;
/**
* EvalContext that represents a union between other contexts - result
* of a union operation like (a | b)
*
* @author Dmitri Plotnikov
* @version $Revision$ $Date$
*/
public class UnionContext extends NodeSetContext {
private EvalContext contexts[];
private boolean prepared;
public UnionContext(EvalContext parentContext, EvalContext contexts[]) {
super(parentContext, new BasicNodeSet());
this.contexts = contexts;
}
public int getDocumentOrder() {
return contexts.length > 1 ? 1 : super.getDocumentOrder();
}
public boolean setPosition(int position) {
if (!prepared) {
prepared = true;
BasicNodeSet nodeSet = (BasicNodeSet) getNodeSet();
ArrayList pointers = new ArrayList();
for (int i = 0; i < contexts.length; i++) {
EvalContext ctx = (EvalContext) contexts[i];
while (ctx.nextSet()) {
while (ctx.nextNode()) {
NodePointer ptr = ctx.getCurrentNodePointer();
if (!pointers.contains(ptr)) {
<CHANGES><SCANS>.EMPTY_LIST;
}
this.nodeTest = nodeTest;
this.reverse = reverse;
}
public NodePointer getNodePointer() {
if (child == null) {
if (!setPosition(1)) {
return null;
}
position = 0;
}
return new JDOMNodePointer(parent, child);
}
public int getPosition() {
return position;
}
public boolean setPosition(int position) {
while (this.position < position) {
if (!next()) {
return false;
}
}
while (this.position > position) {
if (!previous()) {
return false;
}
}
return true;
}
/**
* This is actually never invoked during the normal evaluation
* of xpaths - an iterator is always going forward, never backwards.
* So, this is implemented only for completeness and perhaps for
* those who use these iterators outside of XPath evaluation.
*/
private boolean previous() {
position--;
if (!reverse) {
while (--index >= 0) {
child = children.get(index);
if (testChild()) {
return true;
}
}
}
else {
for (; index < children.size(); index++) {
child = children.get(index);
if (testChild()) {
return true;
}
}
}
return false;
}
private boolean next() {
position++;
if (!reverse) {
if (position == 1) {
index = 0;
if (child != null) {
index = children.indexOf(child) + 1;
}
}
else {
index++;
}
for (; index < children.size(); index++) {
child = children.get(index);
if (testChild()) {
return true;
}
}
return false;
}
else {
if (position == 1) {
index = children.size() - 1;
if (child != null) {
index = children.indexOf(child) - 1;
}
}
else {
index--;
}
for (; index >= 0; index--) {
child = children.get(index);
if (testChild()) {
return true;
}
}
return false;